当前位置:   article > 正文

C++之stringstream类-基本用法详解_c++ stringstream

c++ stringstream

 一、介绍

<sstream> 定义了三个类:istringstream、ostringstream 和 stringstream,分别用来进行流的输入、输出和输入输出操作, stringstream 主要用来进行数据类型转换。由于 stringstream 使用 string 对象来代替字符数组(snprintf方式),可以避免缓冲区溢出的危险;而且,因为传入参数和目标对象的类型会被自动推导出来,所以不存在错误的格式化符的问题。

在C++中,stringstream 是一个流类,它允许你将字符串当做流进行读写操作。它是 <sstream> 头文件中定义的一部分,包含了 istringstream(用于输入),ostringstream(用于输出)和 stringstream(既可以用于输入也可以用于输出)。

stringstream 主要被用于以下几种情况:

  • 字符串和其他数据类型之间的转换。
  • 从字符串中逐个提取数据。
  • 向字符串中逐个插入数据。
  • 作为一般的字符串处理工具。
二、stringstream分割字符串

2.1 如果是空格,可以直接分割

  1. #include <string>
  2. #include <sstream>
  3. #include <iostream>
  4. using namespace std;
  5. int main()
  6. {
  7. string str = "i am a boy";
  8. stringstream is(str);
  9. string s;
  10. while (is >> s) { // 输出流
  11. cout << s << endl;
  12. }
  13. system("pause");
  14. return 0;
  15. }


使用getline()函数输出流

  1. #include <iostream>
  2. #include <sstream>
  3. #include <string>
  4. #include <fstream>
  5. using namespace std;
  6. int main() {
  7. // string str = "i am a boy";
  8. string sss = "1 2 3 4 5 6"; //有空格的字符串,其输出也是一行有空格的数:1 2 3 4 5 6
  9. // string sss = "123456"; //此种无空格的字符串,输出也是无空格:123456
  10. // stringstream is(str);
  11. stringstream is(sss); //
  12. string s;
  13. // while (is >> s) { // 输出流
  14. // cout << s << endl;
  15. // }
  16. while (getline(is,s,',')) { // 输出流 //即使去掉字符‘,’,其输出也是带空格分割的
  17. cout << s << endl;
  18. }
  19. system("pause");
  20. return 0;
  21. }

输出:

1 2 3 4 5 6

补充:

使用getline与stringstream分割字符串

1 getline
头文件:
getline()的原型是istream& getline ( istream &is , string &str , char delim );
其中 istream &is 表示一个输入流,
例如,可使用cin;
string str ; getline(cin ,str)
也可以使用 stringstream
stringstream ss(“test#”) ; getline(ss,str)
char delim表示遇到这个字符停止读入,通常系统默认该字符为’\n’,也可以自定义字符

2 字符串分割

当我们直接利用getline(),自定义字符,从cin流中分割字符,例如
输入 “one#two”

  1. string str;
  2. ss << str2;
  3. while (getline(cin, str, '#'))
  4. cout << str<< endl;
  5. system("pause");
  6. return 0;
  7. 输出:
  8. one
  1. #include <iostream>
  2. #include <sstream>
  3. #include <string>
  4. #include <fstream>
  5. using namespace std;
  6. int main()
  7. {
  8. string str;
  9. string str_cin("one#two#three");
  10. stringstream ss;
  11. ss << str_cin;
  12. //将流ss中的字符串以分隔符#输出到字符串str中去
  13. while (getline(ss, str, '#'))
  14. cout << str<< endl;
  15. system("pause");
  16. return 0;
  17. }
  18. 输出:
  19. one
  20. two
  21. three
  1. int main()
  2. {
  3. string str;
  4. string str_cin("one#two#three");
  5. // string str_cin("one two three");//如果str_cin的初始值为one two three 以空格符的形式隔开,则最终输出则为一行:one two three
  6. stringstream ss;
  7. ss << str_cin; //将"one#two#three" 写入到流对象s中
  8. while (getline(ss, str)) //去掉分割字符‘#’
  9. cout << str<< endl;
  10. system("pause");
  11. return 0;
  12. }
  13. 输出:
  14. one#two#three
  15. // string str_cin("one # two # three"); //用空格分开,则输出为:two 和three前分别有空格
  16. one
  17. two
  18. three

2.2 如果输入的字符串要分别输出的话,必须使用 sstream.str("")来清空 stringstream

  1. stringstream sstream;
  2. int first, second;
  3. // 插入字符串
  4. sstream << "456"; // 转换为int类型
  5. sstream >> first;
  6. cout << first << endl;
  7. // 清空 sstream
  8. sstream.str("") //等价于sstream.str(std::string());
  9. sstream.clear(); // 清除eofbit标志位
  10. // 插入bool值
  11. sstream << true; // 转换为int类型
  12. sstream >> second; // << 代表输入(写入), >> 代表输出(读取)
  13. cout << second << endl;

输出:

三、字符串的拼接

stringstream 中存放多个字符串,实现多个字符串拼接

  1. #include <string>
  2. #include <sstream>
  3. #include <iostream>
  4. using namespace std;
  5. int main() {
  6. stringstream sstream;
  7. // 将多个字符串放入 sstream 中
  8. cout << "开始拼接" << endl;
  9. sstream << "first" << " " << "string,";
  10. sstream << " second string";
  11. cout << "最后的结果 is: " << sstream.str() << endl;
  12. cout << "清空 sstream" << endl;
  13. sstream.str("");
  14. sstream << "third string";
  15. cout << "After clear, 最后的结果 is: " << sstream.str() << endl
  16. return 0;
  17. }

逐个提取数据

你可以使用 stringstream 来逐个提取以特定格式存储在字符串中的数据,例如:

  1. #include <sstream>
  2. #include <iostream>
  3. int main() {
  4. std::string data = "John 25 Programmer";
  5. std::stringstream ss(data);
  6. std::string name;
  7. int age;
  8. std::string occupation;
  9. ss >> name >> age >> occupation;
  10. std::cout << "Name: " << name << std::endl;
  11. std::cout << "Age: " << age << std::endl;
  12. std::cout << "Occupation: " << occupation << std::endl;
  13. }

输出:

Name: John
Age: 25
Occupation: Programmer
 

这里,stringstream 会根据空白字符自动分割字符串,并按照顺序将值赋给相应的变量。

使用 stringstream 进行类型转换

以下是一个使用 stringstream 将数字转换为字符串的例子:

  1. #include <sstream>
  2. #include <iostream>
  3. #include <string>
  4. int main() {
  5. int num = 123;
  6. std::stringstream ss; //此处将int数据插入到流中,不能使用stringstream ss(num),必须使用下面的方式ss << num ,否则插入为空,但是如果是string类型的可以这样使用。
  7. ss << num; // 将整数放入流中
  8. std::string str = ss.str(); // 使用str()函数 从流中提取字符串
  9. std::cout << str << std::endl; // 输出:123
  10. }
  11. 注意:
  12. 利用to_string将整数转化为string类字符串也可以
  13. string s=to_string(int x);
  14. s.size();

反过来,也可以将字符串转换为数值类型:

  1. int main()
  2. {
  3. string line;
  4. while(getline(cin, line))
  5. {
  6. int sum = 0, x;
  7. stringstream ss(line); // 将 line 复制到 stringstream ss 中
  8. while(ss >> x) sum += x; // 相当于输入一个个的单词,自动将其转化换为数字
  9. cout << sum << "\n";
  10. }
  11. return 0;
  12. }
  1. #include <sstream>
  2. #include <iostream>
  3. #include <string>
  4. int main() {
  5. std::string str = "456";
  6. std::stringstream ss(str); // 初始化stringstream ,相当于将str复制到流对象中去
  7. int num;
  8. ss >> num; // 从流中提取整数
  9. std::cout << num << std::endl; // 输出:456
  10. }

二、关于stringstream基本用法

        stringstream类是通过将字符串与流对象相关联来工作的。它可以将字符串内容读取到流对象中,也可以将流对象的内容转换为字符串。

        stringstream是C++中非常有用的工具,可以方便地处理字符串的输入和输出。通过将字符串与流对象相关联,我们可以轻松地将字符串数据转换为其他类型(如整数或浮点数),或者将其他类型的数据转换为字符串。

2.1、创建对象

创建一个 stringstream 对象,可以使用默认构造函数。

std::stringstream ss;

2.2、写入数据

使用流操作符 << 将不同类型的数据写入 stringstream 中。可以连续写入多个数据。

  1. int num = 42;
  2. std::string str = "Hello, World!";
  3. float pi = 3.14159;
  4. ss << "The number is: " << num << "\n";
  5. ss << "The string is: " << str << "\n";
  6. ss << "The value of pi is: " << pi << "\n";

2.3、获取字符串

使用 str() 成员函数可以获取拼接后的字符串。

std::string result = ss.str();

2.4 字符串到流对象

要将字符串放入stringstream对象中,可以使用<<运算符。以下示例将字符串"Hello, World!"放入stringstream对象中:

  1. std::stringstream ss;
  2. std::string str = "Hello, World!";
  3. ss << str;

现在,字符串"Hello, World!"已经被放入了stringstream对象ss中。

2.5、流对象到字符串

要从stringstream对象中提取内容并将其转换为字符串,可以使用>>运算符。以下示例从stringstream对象中提取内容并将其存储在字符串变量中:

  1. std::stringstream ss;
  2. std::string str;
  3. ss >> str;

现在,stringstream对象ss中的内容已被提取并存储在字符串变量str中。

声明:本文仅为学习stringstream类使用方法总结,欢迎批评指证,转载请注明出处!

参考:

1. C++ 中的 stringstream 的简单介绍以及使用_stringstream ss(valuestr);-CSDN博客

2. C++之stringstream类_c++ stringstream-CSDN博客

3. c++ stringstream-CSDN博客

4. 【STL21】C++中的stringstream(字符串流)_c++ stringstream头文件-CSDN博客

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/盐析白兔/article/detail/736006
推荐阅读
  

闽ICP备14008679号