ostringstream的使用

From: http://blog.csdn.net/liminled/article/details/6645451

在std中还有个ostringstream这么方便的格式化输出的工具
 

[cpp] view plain copy
  1. std::string generate()  
  2. {  
  3.     ostringstream osm;  
  4.     osm << "xxxx " << 1111;  
  5.       
  6.     // 清空之前的内容  
  7.     osm.str("");  
  8.       
  9.     // 浮点数转换限制  
  10.     double tmp = 123.1234554351236234;  
  11.     osm.precision(15);  
  12.     osm.setf(std::ios::fixed); // 将浮点数的位数限定为小数点之后的位数  
  13.     osm << tmp;   
  14.   
  15.     return osm.str();  
  16. }  

需要包含的头文件:

[html] view plain copy
  1. #include <sstream>   

 

ostringstream作用、主要接口:

[cpp] view plain copy
  1. Output string stream class  
  2.   
  3. ios_base  ios  ostream  ostringstream   
  4.   
  5. ostringstream provides an interface to manipulate strings as if they were output streams.  
  6.   
  7. The objects of this class maintain internally a pointer to a stringbuf object that can be obtained/modified by calling member rdbuf. This streambuf-derived object controls a sequence of characters (string) that can be obtained/modified by calling member str.  
  8.   
  9.   
  10. Public members  
  11. (constructor) Construct an object and optionally initialize its content (constructor member)   
  12. rdbuf Get/set the associated stringbuf object (public member function)   
  13. str Get/set the associated string object (public member function)

你可能感兴趣的:(C++)