A stringstream associates a string object with a stream allowing you to read from the string as if it were a stream (like cin).
Basic methods are –
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,
stringstream class is extremely useful in parsing input.
Applications :
- Count number of words in a string
Examples: Input : Asipu Pawan Kumar Output : 3 Input : Geeks For Geeks Ide Output : 4
// CPP program to count words in a string// using stringstream.#include <bits/stdc++.h>usingnamespacestd;intcountWords(string str){// breaking input into word using string streamstringstream s(str);// Used for breaking wordsstring word;// to store individual wordsintcount = 0;while(s >> word)count++;returncount;}// Driver codeintmain(){string s ="geeks for geeks geeks ""contribution placements";cout <<" Number of words are: "<< countWords(s);return0;}chevron_rightfilter_noneOutput:
Number of words are: 6
- Print frequencies of individual words in a string
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// CPP program to demonstrate use of stringstream// to count frequencies of words.#include <bits/stdc++.h>usingnamespacestd;voidprintFrequency(string st){// each word it mapped to it's frequencymap<string,int> FW;stringstream ss(st);// Used for breaking wordsstring Word;// To store individual wordswhile(ss >> Word)FW[Word]++;map<string,int>::iterator m;for(m = FW.begin(); m != FW.end(); m++)cout << m->first <<" -> "<< m->second <<"\n";}// Driver codeintmain(){string s ="Geeks For Geeks Ide";printFrequency(s);return0;}chevron_rightfilter_noneOutput:
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 contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
Rated as one of the most sought after skills in the industry, own the basics of coding with our C++ STL Course and master the very concepts by intense problem-solving.
Recommended Posts:
- StringStream in C++ for Decimal to Hexadecimal and back
- Removing spaces from a string using Stringstream
- Find words which are greater than given length k using stringstream
- C++ string class and its applications
- C++ String Class and its Applications | Set 2
- MakeFile in C++ and its applications
- strchr() function in C++ and its applications
- INT_MAX and INT_MIN in C/C++ and Applications
- Reference to a pointer in C++ with examples and applications
- Applications of Pointers in C/C++
- Applications of String Matching Algorithms
- unordered_multiset and its uses
- C++ bitset and its application
- is_permutation() in C++ and its application for anagram search
- isspace() in C/C++ and its application to count whitespace characters
- iscntrl() in C++ and its application to find control characters
- Find sum of a number and its maximum prime factor
- Longest Palindrome in a String formed by concatenating its prefix and suffix
- Macros and its types in C/C++
- unordered_multimap and its application

