c++: stringstream的使用
来源:互联网 发布:短信群发软件哪个好 编辑:程序博客网 时间:2024/05/15 17:07
使用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();}
1 0
- c++: stringstream的使用
- stringstream对象的使用
- stringstream对象的使用
- stringstream 的使用注意事项
- stringstream的函数使用
- stringstream的使用
- stringstream,istringstream,ostringstream的使用
- 【C++】使用stringstream对象简化类型转换
- c++:stringstream
- C++:stringstream
- C++:stringstream
- stringstream 使用
- stringstream使用
- stringstream使用
- C++stringstream的clear()清空误区
- <C++>istringstream, ostringstream, stringstream的用法
- C++stringstream的clear()清空误区
- stringstream的使用_@pegasus.rutgers.edu
- 基于传统复制模式下的主从同步搭建测试
- 个人博客koalahl.github.io
- oracle 中 in函数
- Android 从framework层到HAL层调用实现(蓝牙串口)
- android 定义颜色color时6位或8位值的区别
- c++: stringstream的使用
- 二叉树系列(1)已知二叉树的中序遍历和前序遍历,如何求后序遍历
- iOS9的调整
- 编译链接、cmake和编译opencv
- iOS 委托 协议
- 把字符串中的空格替换为"%20"
- 20160427-Android&Tomcat-RAS加密解密文本
- 工业级 RTU 无线路由器
- 如何判断NSDictionary是否包含某个键