你有被stringstream坑过吗?

stringstream常用来安全的格式化若干个字符串,数值到一个缓冲区, 而不用担心溢出, 可以用来取代snprintf. 
但是很多人都在使用stringstream的时候遇到因为stringstream内部的缓冲区没有正确的清空导致的问题.

那么把stringstream类内部的缓冲区正确的清空方式是什么呢?
stringstream ss;
答案是: ss.str("") 方法.
另外,如果需要把格式化后的字符串通过>>输出到字符串, 必须每次都调用clear()方法!

所以, 保险期间, 每次缓冲区格式化后, 都通过clear(), str("") 两个函数都调用, 把stingstream类复位.


PS1: 网上有一些讨论, 说ss.str("")方法不管用, 而通过 ss.str().clear(); 这可能是c++标准库的实现方法不一致导致. 可以自行看下代码库引用的sstream文件的源码.
       在我的linux机器上, /usr/include/c++/4.1.0/sstream, 以及vs2008的实现, 都是和本文是一致的.
PS2: 注意str() 和 str("") 的区别
       str() 是返回内部缓冲区的一个copy, str("") 是清空内部缓冲区.

测试程序:

[cpp]  view plain  copy
  1. <p>#include <sstream>  
  2. #include <stdio.h>  
  3. using namespace std;  
  4. int main()  
  5. {  
  6.     stringstream ss;  
  7.     string result;  
  8.     int n=1;  
  9.     ss.clear();                                                                                                                                                                    
  10.     ss<<n;  
  11.     ss>>result;  
  12.     printf("result : %s, str : %s\n", result.c_str(), ss.str().c_str());</p><p>    n=2;  
  13.     ss.clear();  
  14.     ss<<n;  
  15.     ss>>result;  
  16.     printf("result : %s, str : %s\n", result.c_str(), ss.str().c_str());</p><p>    n=3;  
  17.     ss.str("");  
  18.     ss<<n;  
  19.     ss>>result;  
  20.     printf("result : %s, str : %s\n", result.c_str(), ss.str().c_str());</p><p>    n=4;  
  21.     ss.clear();  
  22.     ss.str("");  
  23.     ss<<n;  
  24.     ss>>result;  
  25.     printf("result : %s, str : %s\n", result.c_str(), ss.str().c_str());  
  26. }</p>  


 

测试结果:
result : 1, str : 1
result : 2, str : 12 // 调用了clear(), 没有调用str(""), 结果错误.
result : 2, str :    // 调用了 str(""), 没有调用clear(), 结果错误.
result : 4, str : 4 // 调用了 clear()和str(""), 结果正确.

 

附上str("")和str()的内部实现:

[cpp]  view plain  copy
  1.   /**  
  2.    *  @brief  Setting a new buffer. 
  3.    *  @param  s  The string to use as a new sequence. 
  4.    *    
  5.    *  Deallocates any previous stored sequence, then copies @a s to 
  6.    *  use as a new one. 
  7.   */    
  8.   void  
  9.   str(const __string_type& __s)  
  10.   {     
  11. // Cannot use _M_string = __s, since v3 strings are COW.  
  12. _M_string.assign(__s.data(), __s.size());  
  13. _M_stringbuf_init(_M_mode);  
  14.   }  


 

[cpp]  view plain  copy
  1.   // Get and set:  
  2.   /** 
  3.    *  @brief  Copying out the string buffer. 
  4.    *  @return  A copy of one of the underlying sequences. 
  5.    * 
  6.    *  "If the buffer is only created in input mode, the underlying 
  7.    *  character sequence is equal to the input sequence; otherwise, it 
  8.    *  is equal to the output sequence." [27.7.1.2]/1 
  9.   */  
  10.   __string_type  
  11.   str() const  
  12.   {  
  13. __string_type __ret;  
  14. if (this->pptr())  
  15.   {  
  16.     // The current egptr() may not be the actual string end.  
  17.     if (this->pptr() > this->egptr())  
  18.       __ret = __string_type(this->pbase(), this->pptr());  
  19.     else  
  20.       __ret = __string_type(this->pbase(), this->egptr());  
  21.   }  
  22. else  
  23.   __ret = _M_string;  
  24. return __ret;  
  25.   }  
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值