c++: stringstream的使用

使用stringstream需引用头文件< sstream>.

常用方法:

  1. string str() const;
    void str(const string& s);
    第一种调用以string形式返回当前流内容的备份.
    第二种调用则抛弃所有之前的内容, 将s内容作为当前流内容.
  2. iostate rdstate() const;
    返回当前流内部的错误状态标识(error state flags).
  3. void clear(iostate state = goodbit);
    设置错误状态标识(error state flags), 默认为0(goodbit).

注意事项:

  1. 若当前流的state不为0(goodbit), 则读取或写入都会失败.
  2. clear()方法仅能清除error state flags, 对流内容没有影响.
  3. str(const string& s)方法可以设置流内容, 但不影响error state flags.
  4. seekg()在C++11和C++98两种标准中有不同的实现, 最好手动clear()保证功能统一.
  5. 根据stream去理解stringstream, 而不要根据stl.

示例代码:

#include <sstream>
#include <iostream>

using namespace std;

// cplusplus reference url:
// http://www.cplusplus.com/reference/sstream/stringstream/
// http://www.cplusplus.com/reference/ios/ios/rdstate/

void main()
{
    stringstream stream;
    int n1, n2, n3;

    cout << "1. if stringstream state flag is not zero, " << endl
         << "   stringstream can not accept input stream." << endl;

    stream << "123"; // success, input first num.
    cout << stream.rdstate() << " "
         << stream.str() << endl;

    stream >> n1; // success, output first num. state is set to eofbit.
    cout << stream.rdstate() << " "
         << stream.eof() << " "
         << n1 << " " 
         << stream.str() << endl;

    stream << "789"; // fail, input second num, while state is not 0. state is set to eofbit|badbit.
    cout << stream.rdstate() << " "
         << stream.eof() << " " 
         << stream.bad() << " "
         << stream.str() << endl;

    cout << endl
         << "2. clear() can clear state flag, " << endl
         << "   but has no effect on actual string buf." << endl;

    stream.clear(); // clear state mark.
    cout << stream.rdstate() << " "
         << stream.eof() << " "
         << stream.str() << endl;

    stream << "789"; // success, input second num.
    cout << stream.rdstate() << " "
         << stream.eof() << " "
         << stream.str() << endl;

    stream >> n2; // success, output second num. state is set to eofbit.
    cout << stream.rdstate() << " "
         << stream.eof() << " "
         << n2 << " "
         << stream.str() << endl;

    cout << endl
         << "3. C++11: seekg() clears the eofbit flag, if set before the call." << endl
         << "   C++98: if the eofbit flag is set before the call," << endl
         << "          the function fails (sets failbit and returns)" << endl;

    stream.seekg(stream.beg);
    cout << stream.rdstate() << " "
         << stream.eof() << " "
         << stream.str() << endl;

    stream >> n2; // success, output second num from begining. state is set to eofbit.
    cout << stream.rdstate() << " "
         << stream.eof() << " "
         << n2 << " "
         << stream.str() << endl;

    cout << endl
         << "4. str(&) has no effect on error state flags" << endl;

    // stream.clear();
    stream.str("456"); // set third num, no change about error state flags.
    cout << stream.rdstate() << " "
         << stream.eof() << " "
         << stream.str() << endl;

    stream >> n3; //fail, output third num, while state is not 0. state is set to eofbit|failbit.
    cout << stream.rdstate() << " "
         << stream.eof() << " "
         << stream.fail() << " "
         << n3 << " "
         << stream.str() << endl;

    stream.clear();
    stream >> n3; //success, output third num.
    cout << stream.rdstate() << " "
         << stream.eof() << " "
         << n3 << " "
         << stream.str() << endl;

    cin.get();
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值