字符串getline()——整行读入函数
题单
练习 1:读取一句话并输出
练习 2:按逗号拆分多个单词
练习 3:按空格拆分多个单词
练习 4:输入多行,直到输入 "END" 为止
This is line 1
This is line 2
END输出:
This is line 1
This is line 2
练习 5:读取学生姓名和成绩(冒号分隔)
Alice:90
Bob:85
Charlie:78输出:
Name: Alice, Score: 90
Name: Bob, Score: 85
Name: Charlie, Score: 78
升级练习 1:统计每一行有多少个单词
输入示例:
This is a test
C++ is powerful
END输出示例:
Line 1: 4 words
Line 2: 3 words
升级练习 2:学生信息录入 + 冒号分割 + 成绩排序输出
输入示例:
Alice:88
Bob:95
Charlie:78输出示例:
Bob 95
Alice 88
Charlie 78
升级练习 3:按分号分割多行内容并过滤指定关键字
输入示例:
hello world;this is a test;nice day
great;test example;sunshine
END输出示例:(仅输出没有test的句子)
hello world
nice day
great
sunshine
代码
1.一句话读入:
#include <iostream>
#include <string>
using namespace std;
int main() {
string s;
getline(cin, s); // 读取整行
cout << "You entered: " << s << endl;
return 0;
}
2.按逗号拆分单词
#include <iostream>
#include <sstream>
#include <string>
using namespace std;
int main() {
string s;
getline(cin, s); // 输入:apple,banana,grape,orange
stringstream ss(s);
string word;
while (getline(ss, word, ',')) { // 按 , 分割
cout << word << endl;
}
return 0;
}
3.按空格:
#include <iostream>
#include <sstream>
#include <string>
using namespace std;
int main() {
string s;
getline(cin, s); // 输入:C++ is a powerful language
stringstream ss(s);
string word;
while (ss >> word) { // 按空格自动分割
cout << word << endl;
}
return 0;
}
4.多行输入:
#include <iostream>//多行输入,接着输出
#include <string>
using namespace std;
int main() {
string line;
while (getline(cin, line)) {
if (line == "END") break; // 输入END时停止
cout << line << endl;
}
return 0;
}
#include<iostream>//多行输入,输入完输出
#include<string>
#include<vector>
using namespace std;
int main() {
vector<string> s;
string line;
while ( getline(cin, line)) {
if(line=="end") break;
s.push_back(line);
}
for (const auto& l : s) {
cout << l << endl;
}
return 0;
}
5.冒号和换行:
#include <iostream>
#include <string>
#include <sstream>
#include <vector>
using namespace std;
int main() {
vector<string> s0;
string line;
// 接收全部输入
while (getline(cin, line)) {
if (line.empty()) break;
s0.push_back(line);
}
// 处理并输出每行数据
for (const auto& s : s0) {
stringstream ss(s);
string name, score;
if (getline(ss, name, ':') && getline(ss, score)) {
cout << "Name: " << name << ", Score: " << score << endl;
}
}
return 0;
}
升级练习1:
#include <iostream>
#include <string>
#include <sstream>
#include <vector>
using namespace std;
int main() {
vector<string> s0;
string line;
// 接收全部输入
while (getline(cin, line)) {
if (line.empty()) break;
s0.push_back(line);
}
vector<int> cnt;// 处理
for (const auto& s : s0) {
stringstream ss(s);
string word;
int count=0;
while(ss>>word){
count++;
}
cnt.push_back(count);
}
for (const auto& i :cnt){
cout<<i<<"个"<<endl;
}
return 0;
}
升级练习2:
#include <iostream>
#include <sstream>
#include <vector>
#include <algorithm>
using namespace std;
struct Student {
string name;
int score;
};
bool cmp(Student a, Student b) {
return a.score > b.score; // 降序排序
}
int main() {
string line;
vector<Student> students;
while (getline(cin, line)) {
if (line.empty()) break;
stringstream ss(line);
string name,score_str;
if (getline(ss, name, ':') && getline(ss, score_str)) {
Student s;
s.name = name;
s.score = stoi(score_str);//字符串转换成十进制数字
students.push_back(s);
}
}
sort(students.begin(), students.end(), cmp);//倒序输出
for (const auto& s : students) {
cout << s.name << " " << s.score << endl;
}
return 0;
}
升级练习3:
#include <iostream>
#include <sstream>
#include <string>
using namespace std;
bool contains_test(const string& s) {
return s.find("test") != string::npos;
}
int main() {
string line;
while (getline(cin, line)) {
if (line == "END") break;
stringstream ss(line);
string phrase;
while (getline(ss, phrase, ';')) {
if (!contains_test(phrase)) {
cout << phrase << endl;
}
}
}
return 0;
}
知识整理与总结归纳:
1.常用搭配:
头文件:sstream\string\vector\algorithm
while(getline(cin,line))//读取并接收输入的每一行
while(getline(ss,line,','))//按照某个特定的符号读取并接受字符串
2.终止条件:
if (line=='end' ) ; if( ! line.empty())
3.字符串与进制数字的转换函数
string a=to_string(365)//a是字符串365
int b=stoi(a,0,n)//将 n 进制的字符串转化为十进制,默认是10
//int b=stoi(a)
4.数据类型检测函数
typeid(变量).name();//在头文件<typeinfo>中
更多推荐



所有评论(0)