1.介绍

stringstream 将字符串对象与流相关联,允许从字符串中读取,就好像它是一个流(如 cin)

  1. clear() 清除流
  2. str() 获取和设置其内容存在于流中的字符串对象。
  3. 运算符 << 将字符串添加到 stringstream 对象。
  4. 运算符 >> stringstream 对象中读取内容。

2.应用

2.1 计算字符串中的单词数

  1. // CPP program to count words in a string using stringstream.
  2. #include <bits/stdc++.h>
  3. using namespace std;
  4. int countWords(string str)
  5. {
  6. // breaking input into word using string stream
  7. stringstream s(str); // Used for breaking words
  8. string word; // to store individual words
  9. int count = 0;
  10. while (s >> word)
  11. count++;
  12. return count;
  13. }
  14. // Driver code
  15. int main()
  16. {
  17. string s = "geeks for geeks geeks "
  18. "contribution placements";
  19. cout << " Number of words are: " << countWords(s);
  20. return 0;
  21. }

2.2 打印字符串中单个单词的频率

  1. // CPP program to demonstrate use of stringstream
  2. // to count frequencies of words.
  3. #include <bits/stdc++.h>
  4. using namespace std;
  5. void printFrequency(string st)
  6. {
  7. // each word it mapped to it's frequency
  8. map<string, int> FW;
  9. stringstream ss(st); // Used for breaking words
  10. string Word; // To store individual words
  11. while (ss >> Word)
  12. FW[Word]++;
  13. map<string, int>::iterator m;
  14. for (m = FW.begin(); m != FW.end(); m++)
  15. cout << m->first << " -> "
  16. << m->second << "\n";
  17. }
  18. // Driver code
  19. int main()
  20. {
  21. string s = "Geeks For Geeks Ide";
  22. printFrequency(s);
  23. return 0;
  24. }

2.3 使用 Stringstream 从字符串中删除空格

代码一(更优)

  1. // C++ program to remove spaces using stringstream
  2. #include <bits/stdc++.h>
  3. using namespace std;
  4. // Function to remove spaces
  5. string removeSpaces(string str)
  6. {
  7. stringstream ss;
  8. string temp;
  9. // Storing the whole string
  10. // into string stream
  11. ss << str;
  12. // Making the string empty
  13. str = "";
  14. // Running loop till end of stream
  15. while (!ss.eof()) {
  16. // Extracting word by word from stream
  17. ss >> temp;
  18. // Concatenating in the string to be
  19. // returned
  20. str = str + temp;
  21. }
  22. return str;
  23. }
  24. // Driver function
  25. int main()
  26. {
  27. // Sample Inputs
  28. string s = "This is a test";
  29. cout << removeSpaces(s) << endl;
  30. s = "geeks for geeks";
  31. cout << removeSpaces(s) << endl;
  32. s = "geeks quiz is awesome!";
  33. cout << removeSpaces(s) << endl;
  34. s = "I love to code";
  35. cout << removeSpaces(s) << endl;
  36. return 0;
  37. }

代码二

  1. // C++ program to remove spaces using stringstream
  2. // and getline()
  3. #include <bits/stdc++.h>
  4. using namespace std;
  5. // Function to remove spaces
  6. string removeSpaces(string str)
  7. {
  8. // Storing the whole string
  9. // into string stream
  10. stringstream ss(str);
  11. string temp;
  12. // Making the string empty
  13. str = "";
  14. // Running loop till end of stream
  15. // and getting every word
  16. while (getline(ss, temp, ' ')) {
  17. // Concatenating in the string
  18. // to be returned
  19. str = str + temp;
  20. }
  21. return str;
  22. }
  23. // Driver function
  24. int main()
  25. {
  26. // Sample Inputs
  27. string s = "This is a test";
  28. cout << removeSpaces(s) << endl;
  29. s = "geeks for geeks";
  30. cout << removeSpaces(s) << endl;
  31. s = "geeks quiz is awesome!";
  32. cout << removeSpaces(s) << endl;
  33. s = "I love to code";
  34. cout << removeSpaces(s) << endl;
  35. return 0;
  36. }

2.4 C/C+ + 中字符串到数字的转换

将字符串转换为数字有两种常用的方法: 使用 stringstream 类或 sscanf()

stringstream() :这是将数字字符串转换为整数、浮点数或双精度数的简单方法。
以下是使用 stringstream 将 string 转换为 int 的示例程序。

  1. // A program to demonstrate the use of stringstream
  2. #include <iostream>
  3. #include <sstream>
  4. using namespace std;
  5. int main()
  6. {
  7. string s = "12345";
  8. // object from the class stringstream
  9. stringstream geek(s);
  10. // The object has the value 12345 and stream
  11. // it to the integer x
  12. int x = 0;
  13. geek >> x;
  14. // Now the variable x holds the value 12345
  15. cout << "Value of x : " << x;
  16. return 0;
  17. }

sscanf()是一个类似于 scanf() 的 C 风格函数。它从字符串而不是标准输入中读取输入。

  1. #include<stdio.h>
  2. int main()
  3. {
  4. const char *str = "12345";
  5. int x;
  6. sscanf(str, "%d", &x);
  7. printf("\nThe value of x : %d", x);
  8. return 0;
  9. }