C++学习之路(16)---C++ stringstream类 用法概论

来源:互联网 发布:约会倍增术 知乎 编辑:程序博客网 时间:2024/05/22 02:02

C++ stringstream 类的用法


功能一:预定的格式将程序中的数据保存在一个string


C++ stringstream 类是一种十分有用的类,特别是当我们需要在程序中使用字符串和数字数据的时候。要想在程序中使用 stringstream 类,我们需要在源程序文件中包含头文件include<sstream>。stringstream 对象的使用方法与cout对象的使用方法基本相同。stringstream 类提供的函数,将数字化转化为字符串。

当我们需要按预定的格式将程序中的数据保存在一个string 中的时候,可以先创建一个stringstream 对象,并通过运算符 ”<<“ 将数据传递给 stringstream 对象。(这与通过”<<“ 使用cout 对象的方法相同。)接着,我们可以通过调用stringstream 类的函数str() 将对象所包含的内容赋给一个string对象。在一下的程序中,我们先将数据传递给一个stringstream 对象,然后通过该 stringstream 对象将数值赋给一个string 对象。住:cout能使用的所有ios格式标记也可以在stringstream 对象中使用。

[C++] view plain copy
  1. // 如何使用 stringstream   
  2. // 对象生成格式化的 string  
  3.   
  4. #include <iostream>  
  5. #include <string>  
  6. #include <sstream>  
  7. using namespace std;  
  8.   
  9. int main()  
  10. {  
  11.     cout << "\n Welcome to the StringStream Demo program.\n";  
  12.   
  13.     // 构建一些将在string中出现的数据变量  
  14.     // PI 精确到小数点后15位  
  15.     double pi = 3.141592653589793;  
  16.     float dollar = 1.00;  
  17.     int dozen = 12;  
  18.   
  19.     string text;  
  20.   
  21.     // 我们希望tring 的格式如下:  
  22.     // dozen适12,dollar是$1.00  
  23.     // 精确到小数点后10为pi是3.141592653589793  
  24.   
  25.     // 生成stringstream 对象  
  26.     stringstream ss;  
  27.       
  28.     // 现在像使用cout一样使用ss  
  29.   
  30.     ss << " A dozen is "<< dozen << ", a dollar is $ ";  
  31.     ss.setf(ios::fixed);  
  32.     ss.precision(2);  
  33.     ss << dollar << " and \n the value of pi to 10 places is ";  
  34.     ss.precision(10);  
  35.     ss << pi << ".";  
  36.   
  37.     // 现在将ss中的内容赋给一个string对象  
  38.     // 使用str()函数  
  39.   
  40.     text = ss.str();  
  41.     cout << "\nHere is our formatted text string:\n" << text << endl;  
  42.     // 再加入一些信息  
  43.     ss << "\ There are 2 \"+\" in C++.";  
  44.     text = ss.str();  
  45.     cout<< "\nHere is the final string:\n" << text << endl;  
  46.     return 0;  
  47. }  

运行结果图:


功能二:实现类型转换


string到 int / double 的转换

[C++] view plain copy
  1. #include <iostream>  
  2. #include <string>  
  3. #include <sstream>  
  4. using namespace std;  
  5.   
  6. int main()  
  7. {  
  8.     double  rb;    
  9.     int     ri;     // 存储结果  
  10.     string  s;      // 要转化的字符串  
  11.     stringstream ss;  
  12.     s = "123.456789";  
  13.     ss << s;         // 类似 cout  
  14.     ss >> rb;        // 类似 cin  
  15.     cout.precision(10);  
  16.     cout << "string \""<< s << "\" to double object "   
  17.         << rb << endl;  
  18.     s = "654321";  
  19.     ss.clear();      //清空流  
  20.     ss << s;  
  21.     ss >> ri;  
  22.     cout << "string \""<< s << "\" to int object "   
  23.         << ri << endl;  
  24.     return 0;  
  25. }  

运行结果图:



功能三:实现任意类型转换


该功能没多少实际意义,不如static_cast 来的简单。

[C++] view plain copy
  1. #include <iostream>  
  2. #include <string>  
  3. #include <sstream>  
  4. using namespace std;  
  5. template <class t1="" class="" t2="">  
  6.   
  7. int main()  
  8. {  
  9.     double  sb = 123.456;    
  10.     int     ri;     // 存储结果  
  11.     stringstream ss;  
  12.     ss << sb;  
  13.     ss >> ri;  
  14.     cout << ri;  
  15.     return 0;  
  16. }  

原创粉丝点击