C++ How to Read Words From a File With String Stream
A string object instantiated from the string grade is a listing data construction. The listing is a series of characters, and it is appreciated equally such. The C++ string object has many methods. Withal, it lacks certain operations, which are all-time offered if it is seen as a stream. That is where stringstream comes in. Stringstream is a stream, which can be used to:
– Count the number of words in a cord object
– Obtain individual word frequencies in a string object
– Convert a word in text grade in the string object to a number, and vice-versa
Moving characters from the cord object to the C++ program is inputting and represented past the stringstream object. Moving characters from the C++ program to the string object is outputting. Stringstream (i.e. sstream) uses the istringstream and ostringstream classes. An object instantiated from istringstream is responsible for inputting characters to a stringstream. An object instantiated from ostringstream is responsible for outputting characters from a stringstream to a string object.
This tutorial explains what sstream is and how to use it. The target string object is part of the C++ program.
In order to do input, output, or both, in 1 session, the C++ program should brainstorm with:
#include <string>
#include <sstream>
#include <iostream>
Article Content
- Creating a Stringstream Object
- Input Stringstream Operation
- Output Stringstream Operation
- Sending Kickoff Few Words Into Variables
- Counting Number of Words in Cord Literal
- Individual Word Frequencies
- Cord to Number and Vice-Versa
- Conclusion
Creating a Stringstream Object
It is known that stringstream tin can be alleged and applied at the same time in 1 statement. However, that is not the approach in this tutorial. In this tutorial, a stringstream object is instantiated from a grade in one statement and used in another argument.
A stringstream can be instantiated for reading (inputting). A stringstream can be instantiated for writing (outputting). A stringstream can exist instantiated for both reading and writing.
To create a stream object for reading, use:
sstream strm (ios_base:: in ) ;
where strm is the stream object; and "in" of the ios_base form means for reading.
To create a stream object for writing, use:
sstream strm (ios_base:: out ) ;
where strm is the stream object; and "out" of the ios_base grade means for writing.
To create a stream object for reading or writing, use:
sstream strm (ios_base:: in | ios_base:: out ) ;
where "ios_base::in | ios_base::out", is for reading or writing.
Input Stringstream Operation
Text to input to a stringstream from a string object can exist done in 2 means: using the insertion (<<) operator or using the str() fellow member role of the sstream class. The following program illustrates this for both ways:
#include <cord>
#include <sstream>
#include <iostream>
using namespace std;
int main( )
{
stringstream strm1(ios_base:: in ) ;
strm1 << "Nosotros are the world!" ;
string stri2 = "This is the world!" ;
stringstream strm2(ios_base:: in ) ;
strm2 << stri2;
stringstream strm3(ios_base:: in ) ;
strm3.str ( "Mars is next." ) ;
string stri4 = "What well-nigh Jupiter?" ;
stringstream strm4(ios_base:: in ) ;
strm4.str (stri4) ;
return 0 ;
}
The string object can be the literal or the identifier. Note that in the proclamation of the sstream object, "stringstream" is used and not "sstream", though both terms mean the same matter. The term sstream should exist used in the include directive.
Output Stringstream Functioning
A give-and-take is any cord text that does not have whatever space (' ') within. Outputting from a stringstream ways sending a string word from the sstream object to a string object. This needs the extraction operator (>>). The following plan sends a word from a sstream object to a string object:
#include <string>
#include <sstream>
#include <iostream>
using namespace std;
int main( )
{
stringstream strm;
strm << "love" ;
cord stri = "detest" ;
strm >> stri;
cout << stri << endl;
return 0 ;
}
The output is:
Any string value that the string object might take had is replaced. If the stringstream is alleged in a higher place and without any statement, then information technology is for input or output.
If the string value of the stringstream object (buffer) has spaces, then merely the start word will be sent. The sstream member function, str(), has to be used to transport the whole string value, including the spaces. Str() tin can exist used to catechumen a string literal into the sstream content. It tin too exist used to return all the content of the stream buffer, including the spaces, to the string object. The following programme illustrates this:
#include <string>
#include <sstream>
#include <iostream>
using namespace std;
int main( )
{
stringstream strm;
strm << "I love her, but he hates her." ;
string stri;
stri = strm.str ( ) ;
cout << stri << endl;
return 0 ;
}
The output is:
I love her, but he hates her.
Sending First Few Words Into Variables
In the cord,
"I love her, but he hates her."
If the beginning 5 words are to exist represented past the variables, a, b, c, d, e, and then these variables can be made to agree the words. The following code illustrates this:
#include <string>
#include <sstream>
#include <iostream>
using namespace std;
int main( )
{
stringstream strm;
strm << "I dearest her, merely he hates her." ;
string a, b, c, d, e;
strm >> a >> b >> c >> d >> e;
cout << a << ' ' << b << ' ' << c << ' ' << d << ' ' << e << endl;
return 0 ;
}
The output is:
A unmarried character is a word. Notation that the comma has been joined with "her". If the comma had been separated from "her", then it would have been considered as a split up word.
Counting Number of Words in String Literal
In the previous section, only the first 5 words were identified. In club to send all the words to different variables, the number of words has to exist known. If the trouble is only to know the number of words in the sstream buffer, then that tin can exist washed in a while-loop. It is by sending all the words to 1 variable, while counting the number of times the sending is done, and until the stop of the stream (terminate-of-file) is reached. The post-obit lawmaking illustrates this:
#include <string>
#include <sstream>
#include <iostream>
using namespace std;
int main( )
{
stringstream strm;
strm << "I love her, but he hates her." ;
int counter = 0 ;
cord temp;
while ( !strm.eof ( ) ) {
strm >> temp;
counter + = 1 ;
}
cout << counter << endl;
return 0 ;
}
The output is seven. The total stop is attached to the second "her". Notation that the indicator for the end-of-file is the sstream member office, eof().
Individual Give-and-take Frequencies
In the string value,
"I dearest her, and he loves her sis, though he hates her brother."
The word, "her", occurs 3 times, and the frequency of "her" is indicated to exist 3. The word, "he", appears 2 times, and the frequency of "he" is said to exist 2. The residual of the words accept a frequency of 1 each. The frequency of each word can be determined as follows:
Have all the words in a C++ map without repetition. Consider the following statement:
where mp is a map object. The starting time time this statement is encountered in a while-loop, information technology fits in a new key/value pair, whose key is the string give-and-take of the variable temp and whose value is 1. The next time it is encountered in the same while-loop, with this detail cardinal, no new key/value pair is added to the map. The value of this key/value pair is simply incremented.
Then, the strategy is to have this statement in a while-loop and all the words in the ssstream buffer beingness read into a temporary variable. And, each value for each cardinal/value pair in the map finally becomes the frequency of the key (give-and-take). The following program illustrates this:
#include <string>
#include <sstream>
#include <map>
#include <iostream>
using namespace std;
int main( )
{
stringstream strm;
strm << "I dear her and he loves her sister, though he hates her brother." ;
string temp;
map<cord, int > mp;
while (strm >> temp) {
mp[temp] ++ ;
}
for (map<string, int > :: iterator it = mp.begin ( ) ; it ! = mp.terminate ( ) ; it++ )
cout << it- >starting time << " => " << it- >second << endl;
return 0 ;
}
The output is:
I => 1
and => i
brother. => 1
hates => one
he => 2
her => iii
love => 1
loves => 1
sis, => 1
though => ane
String to Number and Vice-Versa
String to Number
To convert a string give-and-take to a number, only send the string word from the sstrream buffer to a variable declared every bit a number. To catechumen to an int, transport the cord give-and-take to an int variable. To convert to a float, ship the string word to a float variable. The following program illustrates these:
#include <cord>
#include <sstream>
#include <iostream>
using namespace std;
int main( )
{
stringstream strm;
strm << " 30 " ;
int myInt;
strm >> myInt;
int intResult = myInt + 10 ;
cout << intResult << endl;
strm << " 2.v " ;
float myfloat;
strm >> myfloat;
float fltResult = myfloat + 0.3 ;
cout << fltResult << endl;
return 0 ;
}
The output is:
Number to String
To convert a number to a string give-and-take, just send the number to the sstream buffer. So, read out the stream buffer into a string object. To convert an int to a string, sent the integer to the sstream. To convert a float to a string, ship the float to the sstream. The post-obit program illustrates these:
#include <string>
#include <sstream>
#include <iostream>
using namespace std;
int main( )
{
stringstream strm1;
int myInt = xxx ;
strm1 << myInt;
string intStr;
strm1 >> intStr;
string intStrRes = intStr + " good" ;
cout << intStrRes << endl;
stringstream strm2;
float myflt = two.five ;
strm2 << myflt;
string fltStr;
strm2 >> fltStr;
string fltStrRes = fltStr + " yep" ;
cout << fltStrRes << endl;
return 0 ;
}
The output is:
Two stream objects accept been used hither for the different number types.
Note: the sstream buffer consists of characters only.
Decision
Stringstream means Cord Stream. It is likewise written as sstream. It is a stream. The target for this stream is a string object. The stream tin can utilize the insertion operator (<<) and the extraction operator (>>). The stream is very helpful for the following purposes: counting the number of words in a string object, obtaining individual word frequencies in a cord object, and converting a give-and-take in text grade in the string object to a number, and vice-versa.
Source: https://linuxhint.com/c-stringstream-and-how-to-use-it/
0 Response to "C++ How to Read Words From a File With String Stream"
Enregistrer un commentaire