C++ string流笔记

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
*/

OSTRINGSTREAM待补充

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值