C++ istringstream,ostringstream,stringstream的使用

x33g5p2x  于2021-09-24 转载在 C/C++  
字(3.1k)|赞(0)|评价(0)|浏览(3615)

一,基本介绍

头文件:/#include

istringstream类用于执行C++风格的串流的输入操作,支持 >> 操作。

ostringstream类用于执行C风格的串流的输出操作,支持 << 操作。

stringstream类可以同时用于C风格的串流的输入输出操作, 同时支持 >> 和 << 操作,所以,stringstream将上述两个类的功能都包括在内。

二,istringstream

构造方法:

(1)

  1. istringstream(string str);

(2)

  1. istringstream istr;
  2. istr.str(string str);

用法:

1,将字符串类型转换为其他类型

  1. #include <iostream>
  2. #include <sstream>
  3. using namespace std;
  4. int main()
  5. {
  6. istringstream istr("123.54");
  7. string str = istr.str();//str()函数返回一个字符串
  8. cout<<str<<endl; //输出 123.54
  9. double d;
  10. istr >> d;
  11. cout << d << endl; //输出 123.54, 字符串被转换为double类型
  12. int a;
  13. istr >> a;
  14. cout << a << endl; //注意,流中的数据已经输出完,这里输出的是随机数
  15. istringstream istr2("123.54");
  16. istr2 >> a;
  17. cout << a << endl; //输出 123
  18. istr2 >> d;
  19. cout << d << endl; //注意,这里输出的是 0.54
  20. return 0;
  21. }

2,以空格为分隔符将字符串分离

  1. #include <iostream>
  2. #include <sstream>
  3. using namespace std;
  4. int main()
  5. {
  6. istringstream istr("12 345 678 912");
  7. string str;
  8. while(istr >> str)//以空格为界,每次读取一部分内容
  9. {
  10. cout<<str<<endl;
  11. }
  12. /* 输出: 12 345 678 912 */
  13. istringstream istr2("26 32.5");
  14. int a;
  15. double b;
  16. istr2 >> a;
  17. cout << a << endl; //输出26
  18. istr2 >> b;
  19. cout << b << endl; //输出32.5
  20. return 0;
  21. }

三,ostringstream

构造方法
(1)

  1. ostringstream(string str);

(2)

  1. ostringstream ostr;
  2. ostr.str(string str);

用法:

构造字符串或修改字符串

  1. #include <iostream>
  2. #include <sstream>
  3. using namespace std;
  4. int main()
  5. {
  6. ostringstream ostr("abcdef");
  7. cout << ostr.str() <<endl; //输出abcdef
  8. ostr.put('1'); //字符'1'替换了字符'a'
  9. cout << ostr.str() << endl; //输出1bcdef
  10. ostr << "23";//字符串"23"替换了字符'b''c'
  11. cout << ostr.str() << endl; //输出123def
  12. ostr << "456789"; //字符串"456789"替换了字符'c''d''e''f'
  13. cout << ostr.str() << endl; //输出 123456789,长度增加
  14. return 0;
  15. }

四,stringstream

构造方法
(1)

  1. stringstream(string str);

(2)

  1. stringstream str;
  2. str.str(string str);

用法:

1,字符串转换为基本数据类型

  1. #include <fstream>
  2. #include <iostream>
  3. #include <sstream>
  4. using namespace std;
  5. int main()
  6. {
  7. //string转为int
  8. stringstream stream;
  9. string str = "45";
  10. int a;
  11. stream << str;
  12. stream >> a;
  13. cout << a << endl; //输出45
  14. stream.clear(); //清空;
  15. //string转为double
  16. string str1 = "5.28";
  17. double d;
  18. stream << str1;
  19. stream >> d;
  20. cout << d << endl; //输出5.28
  21. stream.clear(); //清空;
  22. //string转为char*
  23. string str2 = "first";
  24. char c[10];
  25. stream << str2;
  26. stream >> c;
  27. cout << c << endl;//输出first
  28. return 0;
  29. }

2,基本数据类型转换为字符串

  1. #include <fstream>
  2. #include <iostream>
  3. #include <sstream>
  4. using namespace std;
  5. int main()
  6. {
  7. //整型转为字符串
  8. int a = 10;
  9. string str;
  10. stringstream stream;
  11. stream << a;
  12. stream >> str;
  13. cout << str << endl; //输出10
  14. stream.clear(); //清空
  15. //char* 转为 string
  16. char c[10] = "first";
  17. stream << c;
  18. stream >> str;
  19. cout << str << endl; //输出first
  20. return 0;
  21. }

3,以空格为分隔符将字符串分离

  1. #include <iostream>
  2. #include <sstream>
  3. using namespace std;
  4. int main()
  5. {
  6. stringstream stt("12 345 678 912");
  7. string str;
  8. while(stt >> str)//以空格为界,每次读取一部分内容
  9. {
  10. cout<<str<<endl;
  11. }
  12. /* 输出: 12 345 678 912 */
  13. stringstream stt2("26 32.5");
  14. int a;
  15. double b;
  16. stt2 >> a;
  17. cout << a << endl; //输出26
  18. stt2 >> b;
  19. cout << b << endl; //输出32.5
  20. return 0;
  21. }

相关文章

最新文章

更多