sstream 头文件定义了三个类型来支持内存 IO,这些类型可以向 string 写入数据,从 string 读取数据,就像 string 是一个 IO 流一样。
istringstream 从 string 读取数据,ostringstream 向 string 写入数据,stringstream 既可以从 string 读取数据也可以向 string 写入数据。类似于 fstream,sstream 中定义的类型也都是继承自 iostream 中的类型。除了继承来的操作,sstream 中定义的类型还增加了一些成员来管理与流相关的 string。可以对 stringstream 对象调用这些操作,但不能对其他 IO 类型调用这些操作:
istringstream样例如下:
#include<bits/stdc++.h>
using namespace std;
struct Node{
string name;
vector<string> phones;
void prin()const{
cout<<name<<endl;
for(auto x:phones)cout<<x<<" ";cout<<endl;
}
};
int main(){
string name,line,word;
vector<Node> people;
while(getline(cin,line)){
Node e;
istringstream record(line);
record>>e.name;
while(record>>word) e.phones.push_back(word);
people.push_back(e);
}
for(const auto x:people)x.prin();
}
/*输入数据:
morgan 2017347 72849289
jfsl 7823434
jfkds 7384534 58349578 59308509
*/