关于stringstream重复使用时的问题
2015-03-07 05:34
218 查看
1.clear()与str("")
测试代码
string s1("333.444.555");
string s2("666.777.888");
stringstream ss;
string w1, w2;
ss << s1;
getline(ss, w1, '.');
cout << w1 << " " << ss.str() << endl;
ss.clear(); // ss.str("");
ss << s2;
getline(ss, w2, '.');
cout << w2 << " " << ss.str() << endl;
测试截图
clear()并没有清空string buff
将上述代码中ss.clear()替换为ss.str("");
测试截图
可见string buff被清空了
原因:
str("")的原型和作用
void str (const string& s);
sets s as the contents of the stream, discarding any previous contents. The object preserves its open mode: if this includes ios_base::ate, the writing position is moved to the end of the new sequence.
clear()的原型和作用
void clear (iostate state = goodbit);
Set error state flags
Sets a new value for the stream's internal error state flags.
其实这段代码也是存在问题的,在别的编译器上可能会报错。在【关于stringstream重复使用时的问题2】中。
测试代码
string s1("333.444.555");
string s2("666.777.888");
stringstream ss;
string w1, w2;
ss << s1;
getline(ss, w1, '.');
cout << w1 << " " << ss.str() << endl;
ss.clear(); // ss.str("");
ss << s2;
getline(ss, w2, '.');
cout << w2 << " " << ss.str() << endl;
测试截图
clear()并没有清空string buff
将上述代码中ss.clear()替换为ss.str("");
测试截图
可见string buff被清空了
原因:
str("")的原型和作用
void str (const string& s);
sets s as the contents of the stream, discarding any previous contents. The object preserves its open mode: if this includes ios_base::ate, the writing position is moved to the end of the new sequence.
clear()的原型和作用
void clear (iostate state = goodbit);
Set error state flags
Sets a new value for the stream's internal error state flags.
其实这段代码也是存在问题的,在别的编译器上可能会报错。在【关于stringstream重复使用时的问题2】中。
相关文章推荐
- 关于stringstream重复使用时的问题2
- 关于使用js进行表单提交操作引发的表单重复提交问题
- 关于jquery的slideToggle()效果重复使用的问题
- 关于Hibernate中fatch=eager的bag集合(一个java List)使用Criteria查询出现重复记录的问题
- 关于使用select标签,去掉重复选项的问题。默认首选项不被去除。
- 关于新增元素使用jQuery on()方法重复绑定的问题
- 问个关于VS使用上的问题
- 刚才遇到了关于C#使用外部DLL函数上的char*的问题。
- 关于在struts 框架中使用Filter过滤器解决汉字编码问题
- VC中一个关于宏的使用问题,字符串之间转换,宽字符与普通字符
- 关于GetRecordCount函数的使用问题
- 关于在VB中使用Access数据库出现的问题
- 关于SQL SERVER的内存使用的问题
- 关于您提到使用WebServices时候遇到“基础连接以关闭”的问题
- 关于如何使用TXT、CSV表的问题.
- 关于使用VS.Net2003调试器出现的问题及相关解决方法 (转)
- 关于delphi里面使用QUICK REPORT 打印的种种问题
- 关于使用docuemnt.write问题
- 关于使用VS.Net2003调试器出现的问题及相关解决方法
- 详细描述关于aps.net 下使用水晶报表的问题!(一)