stringstream

A stringstream associates a string object with a stream allowing you to read from the string as if it were a stream (like cin).

接口函数

clear() — to clear the stream
str() — to get and set string object whose content is present in stream.
operator << — add a string to the stringstream object.
operator >> — read something from the stringstream object,

应用

  • 统计字符串中的单词数目
  • 统计字符串中的词频
  • 去除字符串中的空格
  • 字符串转换为数字
    示例来源
// A program to demonstrate the use of stringstream 
#include  
#include  
using namespace std; 
  
int main() 
{ 
    string s = "12345"; 
  
    // object from the class stringstream 
    stringstream geek(s); 
  
    // The object has the value 12345 and stream 
    // it to the integer x 
    int x = 0; 
    geek >> x; 
  
    // Now the variable x holds the value 12345 
    cout << "Value of x : " << x; 
  
    return 0; 
}

你可能感兴趣的:(stringstream)