Stringstream trong C++ là gì

A stringstream associates a string object with a stream allowing you to read from the string as if it were a stream [like cin]. To use stringstream, we need to include sstream header file. The stringstream class is extremely useful in parsing input. 

Basic methods are:

  1. clear[]- To clear the stream.
  2. str[]- To get and set string object whose content is present in the stream. 
  3. operator - Read something from the stringstream object.

Examples:

1. Count the number of words in a string

Examples:

Input: Asipu Pawan Kumar
Output: 3

Input: Geeks For Geeks Ide
Output: 4

Below is the C++ program to implement the above approach-

C++




// C++ program to count words in 

// a string using stringstream.

#include

#include

#include

using namespace std;

 

For-> 1
Geeks-> 2
Ide-> 1
0
For-> 1
Geeks-> 2
Ide-> 1
1

For-> 1
Geeks-> 2
Ide-> 1
2

For-> 1
Geeks-> 2
Ide-> 1
3
For-> 1
Geeks-> 2
Ide-> 1
4

For-> 1
Geeks-> 2
Ide-> 1
3
For-> 1
Geeks-> 2
Ide-> 1
6

For-> 1
Geeks-> 2
Ide-> 1

For-> 1
Geeks-> 2
Ide-> 1
3
For-> 1
Geeks-> 2
Ide-> 1
9

For-> 1
Geeks-> 2
Ide-> 1
3// C++ program to count words in 1

For-> 1
Geeks-> 2
Ide-> 1

For-> 1
Geeks-> 2
Ide-> 1
3// C++ program to count words in 4

For-> 1
Geeks-> 2
Ide-> 1
3// C++ program to count words in 6

 

For-> 1
Geeks-> 2
Ide-> 1
3
For-> 1
Geeks-> 2
Ide-> 1
0 // C++ program to count words in 9

For-> 1
Geeks-> 2
Ide-> 1
3// a string using stringstream.1 // a string using stringstream.2

// a string using stringstream.3// a string using stringstream.4

For-> 1
Geeks-> 2
Ide-> 1
3// a string using stringstream.6 // a string using stringstream.7

// a string using stringstream.8

 

// a string using stringstream.9

For-> 1
Geeks-> 2
Ide-> 1
0 #include 1

For-> 1
Geeks-> 2
Ide-> 1
2

For-> 1
Geeks-> 2
Ide-> 1
3#include 4#include 5

#include 6#include 7#include 8

For-> 1
Geeks-> 2
Ide-> 1
3#include 0#include 1 #include 2

For-> 1
Geeks-> 2
Ide-> 1
3// a string using stringstream.6 #include 5

// a string using stringstream.8

Output

 Number of words are: 6

2. Print frequencies of individual words in a string

Examples:

Input: Geeks For Geeks Quiz Geeks Quiz Practice Practice
Output: For -> 1
             Geeks -> 3
             Practice -> 2
             Quiz -> 2

Input: Word String Frequency String
Output: Frequency -> 1
              String -> 2
              Word -> 1     

Below is the C++ program to implement the above approach-

C++




#include 7

#include 8

#include 9

#include0

#include

#include

#include

using namespace std;

 

#include7 #include8

For-> 1
Geeks-> 2
Ide-> 1
2

For-> 1
Geeks-> 2
Ide-> 1
3using1

For-> 1
Geeks-> 2
Ide-> 1
3using3

For-> 1
Geeks-> 2
Ide-> 1
3using5
For-> 1
Geeks-> 2
Ide-> 1
0using7

For-> 1
Geeks-> 2
Ide-> 1

For-> 1
Geeks-> 2
Ide-> 1
3
For-> 1
Geeks-> 2
Ide-> 1
9

For-> 1
Geeks-> 2
Ide-> 1
3namespace2

For-> 1
Geeks-> 2
Ide-> 1

For-> 1
Geeks-> 2
Ide-> 1
3// C++ program to count words in 4

For-> 1
Geeks-> 2
Ide-> 1
3namespace7

 

For-> 1
Geeks-> 2
Ide-> 1
3// a string using stringstream.1 std;0

// a string using stringstream.3std;2

 

For-> 1
Geeks-> 2
Ide-> 1
3using5
For-> 1
Geeks-> 2
Ide-> 1
0std;6

For-> 1
Geeks-> 2
Ide-> 1
3std;8 std;9

// a string using stringstream.3

For-> 1
Geeks-> 2
Ide-> 1
01
For-> 1
Geeks-> 2
Ide-> 1
02

For-> 1
Geeks-> 2
Ide-> 1
03
For-> 1
Geeks-> 2
Ide-> 1
04
For-> 1
Geeks-> 2
Ide-> 1
05#include 8

// a string using stringstream.8

 

// a string using stringstream.9

For-> 1
Geeks-> 2
Ide-> 1
0 #include 1

For-> 1
Geeks-> 2
Ide-> 1
2

For-> 1
Geeks-> 2
Ide-> 1
3#include 4
For-> 1
Geeks-> 2
Ide-> 1
14#include 8

For-> 1
Geeks-> 2
Ide-> 1
3
For-> 1
Geeks-> 2
Ide-> 1
17

For-> 1
Geeks-> 2
Ide-> 1
3// a string using stringstream.6 #include 5

// a string using stringstream.8

Output

For-> 1
Geeks-> 2
Ide-> 1

Removing spaces from a string using Stringstream
Converting Strings to Numbers in C/C++

This article is contributed by ASIPU PAWAN KUMAR. If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to review-team@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Chủ Đề