使用stringstream需引用头文件< sstream>.
常用方法:
- string str() const;
void str(const string& s);
第一种调用以string形式返回当前流内容的备份.
第二种调用则抛弃所有之前的内容, 将s内容作为当前流内容. - iostate rdstate() const;
返回当前流内部的错误状态标识(error state flags). - void clear(iostate state = goodbit);
设置错误状态标识(error state flags), 默认为0(goodbit).
注意事项:
- 若当前流的state不为0(goodbit), 则读取或写入都会失败.
- clear()方法仅能清除error state flags, 对流内容没有影响.
- str(const string& s)方法可以设置流内容, 但不影响error state flags.
- seekg()在C++11和C++98两种标准中有不同的实现, 最好手动clear()保证功能统一.
- 根据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();
}