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:
Examples: 1. Count the number of words in a string Examples:
Below is the C++ program to implement the above approach- C++
For-> 1 Geeks-> 2 Ide-> 10 For-> 1 Geeks-> 2 Ide-> 11 For-> 1 Geeks-> 2 Ide-> 12 For-> 1 Geeks-> 2 Ide-> 13 For-> 1 Geeks-> 2 Ide-> 14 For-> 1 Geeks-> 2 Ide-> 13 For-> 1 Geeks-> 2 Ide-> 16 For-> 1 Geeks-> 2 Ide-> 17 For-> 1 Geeks-> 2 Ide-> 13 For-> 1 Geeks-> 2 Ide-> 19 For-> 1 Geeks-> 2 Ide-> 13 // C++ program to count words in 1For-> 1 Geeks-> 2 Ide-> 17 For-> 1 Geeks-> 2 Ide-> 13 // C++ program to count words in 4For-> 1 Geeks-> 2 Ide-> 13 // C++ program to count words in 6
For-> 1 Geeks-> 2 Ide-> 13 For-> 1 Geeks-> 2 Ide-> 10 // C++ program to count words in 9For-> 1 Geeks-> 2 Ide-> 13 // a string using stringstream. 1 // a string using stringstream. 2
For-> 1 Geeks-> 2 Ide-> 13 // a string using stringstream. 6 // a string using stringstream. 7
For-> 1 Geeks-> 2 Ide-> 10 #include 1For-> 1 Geeks-> 2 Ide-> 12 For-> 1 Geeks-> 2 Ide-> 13 #include 4#include 5
For-> 1 Geeks-> 2 Ide-> 13 #include 0#include 1 #include 2For-> 1 Geeks-> 2 Ide-> 13 // a string using stringstream. 6 #include 5
Output Number of words are: 6 2. Print frequencies of individual words in a string Examples:
Below is the C++ program to implement the above approach- C++
For-> 1 Geeks-> 2 Ide-> 12 For-> 1 Geeks-> 2 Ide-> 13 using 1For-> 1 Geeks-> 2 Ide-> 13 using 3For-> 1 Geeks-> 2 Ide-> 13 using 5For-> 1 Geeks-> 2 Ide-> 10 using 7For-> 1 Geeks-> 2 Ide-> 17 For-> 1 Geeks-> 2 Ide-> 13 For-> 1 Geeks-> 2 Ide-> 19 For-> 1 Geeks-> 2 Ide-> 13 namespace 2For-> 1 Geeks-> 2 Ide-> 17 For-> 1 Geeks-> 2 Ide-> 13 // C++ program to count words in 4For-> 1 Geeks-> 2 Ide-> 13 namespace 7
For-> 1 Geeks-> 2 Ide-> 13 // a string using stringstream. 1 std; 0
For-> 1 Geeks-> 2 Ide-> 13 using 5For-> 1 Geeks-> 2 Ide-> 10 std; 6For-> 1 Geeks-> 2 Ide-> 13 std; 8 std; 9
For-> 1 Geeks-> 2 Ide-> 101 For-> 1 Geeks-> 2 Ide-> 102 For-> 1 Geeks-> 2 Ide-> 103 For-> 1 Geeks-> 2 Ide-> 104 For-> 1 Geeks-> 2 Ide-> 105 #include 8
For-> 1 Geeks-> 2 Ide-> 10 #include 1For-> 1 Geeks-> 2 Ide-> 12 For-> 1 Geeks-> 2 Ide-> 13 #include 4For-> 1 Geeks-> 2 Ide-> 114 #include 8For-> 1 Geeks-> 2 Ide-> 13 For-> 1 Geeks-> 2 Ide-> 117 For-> 1 Geeks-> 2 Ide-> 13 // a string using stringstream. 6 #include 5
Output For-> 1 Geeks-> 2 Ide-> 1 Removing spaces from a string using Stringstream 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 [email protected]. See your article appearing on the GeeksforGeeks main page and help other Geeks. |