string流
头文件 | 类型 |
---|
sstream | istringstream,wistringstream 从string中读数据 |
| ostringstream,wostringstream 从string中写数据 |
| stringstream,wstringstream 读写string |
string流操作
函数及类型 | 含义 |
---|
sstream fstrm | 创建未绑定string流 |
sstream fstrm(s) | 创建string流并绑定s |
fstrm.str() | 返回strm保存的string拷贝 |
fstrm.str(s) | 将s拷贝到strm中,返回void。 |
样例:
bool valid(const string &num) {
for (const auto s : num) {
if(!isdigit(s))
{
return false;
}
}
return true;
}
struct PersonInfo {
string name;
vector<string> phones;
};
int main() {
string line, word;
vector<PersonInfo> people;
while (getline(cin, line)) {
PersonInfo info;
istringstream record(line);
record >> info.name;
while (record >> word)
info.phones.push_back(word);
people.push_back(info);
}
for (const auto& entry : people) {
ostringstream formatted, badNums;
for (const auto& nums : entry.phones) {
if (!valid(nums)) {
badNums << " " << nums;
}
else {
formatted << " " << nums;
}
}
if (badNums.str().empty()) {
cout << entry.name << ":" << formatted.str() << endl;
}
else {
cerr << "error in "<<entry.name << ":" << badNums.str() << endl;
}
}
return 0;
}