关于i/o stringstream的一些闲谈

头文件:Defined in header
IO类之stringstream
也是一个类模板
stringstream的对象与内存中的string对象建立关联, 往string对象写东西, 或者从string对象读取东西
istringstream的关联是ok的, 但是ostringstream不能依赖于关联

#include <sstream>
#include <iostream>
int main()
{
    int n;
 
    std::istringstream in;  // could also use in("1 2")
    in.str("1 2");
    in >> n;			//i 是读出
    std::cout << "after reading the first int from \"1 2\", the int is "
              << n << ", str() = \"" << in.str() << "\"\n";
 
    std::ostringstream out("1 2");
    out << 3;		//o 是读入
    std::cout << "after writing the int '3' to output stream \"1 2\""
              << ", str() = \"" << out.str() << "\"\n";
 
    std::ostringstream ate("1 2", std::ios_base::ate);
    ate << 3;
    std::cout << "after writing the int '3' to append stream \"1 2\""
              << ", str() = \"" << ate.str() << "\"\n";
     }

in 是将里面的东西可以读出》》
out 是将外面的东西读入《《
两者都有成员函数 .str() 输出其中的字符串
并且注意:

  1. out读入的时候,可以直接读入int类型的数据,因为已经重载过了。/没有指定后面有参数ate时,是在最开始位置覆盖。并且注意是字符串。
  2. 如果std::ostringstream out(“1 2”, std::ios_base::ate); 有定义的话,是从最后面加入
  3. in 关联的时候
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值