Reading in String From File C++ Stringstream
A cord object instantiated from the string class is a list data structure. The list is a series of characters, and information technology is appreciated as such. The C++ string object has many methods. However, it lacks sure operations, which are best offered if it is seen as a stream. That is where stringstream comes in. Stringstream is a stream, which can exist used to:
– Count the number of words in a cord object
– Obtain individual word frequencies in a string object
– Catechumen a give-and-take in text form in the string object to a number, and vice-versa
Moving characters from the string object to the C++ program is inputting and represented by the stringstream object. Moving characters from the C++ program to the cord object is outputting. Stringstream (i.eastward. 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 information technology. The target string object is part of the C++ programme.
In lodge to do input, output, or both, in one session, the C++ program should begin with:
#include <string>
#include <sstream>
#include <iostream>
Commodity Content
- Creating a Stringstream Object
- Input Stringstream Operation
- Output Stringstream Operation
- Sending First Few Words Into Variables
- Counting Number of Words in String Literal
- Individual Discussion Frequencies
- Cord to Number and Vice-Versa
- Conclusion
Creating a Stringstream Object
Information technology is known that stringstream can be declared 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 class in one statement and used in some other statement.
A stringstream tin 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, employ:
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 class 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 be done in two ways: using the insertion (<<) operator or using the str() fellow member function of the sstream class. The following programme illustrates this for both ways:
#include <cord>
#include <sstream>
#include <iostream>
using namespace std;
int main( )
{
stringstream strm1(ios_base:: in ) ;
strm1 << "We are the globe!" ;
string stri2 = "This is the earth!" ;
stringstream strm2(ios_base:: in ) ;
strm2 << stri2;
stringstream strm3(ios_base:: in ) ;
strm3.str ( "Mars is next." ) ;
cord stri4 = "What about Jupiter?" ;
stringstream strm4(ios_base:: in ) ;
strm4.str (stri4) ;
render 0 ;
}
The string object can be the literal or the identifier. Annotation that in the declaration of the sstream object, "stringstream" is used and non "sstream", though both terms mean the same matter. The term sstream should be used in the include directive.
Output Stringstream Operation
A word is any string text that does not have whatsoever space (' ') inside. Outputting from a stringstream means sending a string word from the sstream object to a string object. This needs the extraction operator (>>). The following program sends a give-and-take from a sstream object to a string object:
#include <string>
#include <sstream>
#include <iostream>
using namespace std;
int primary( )
{
stringstream strm;
strm << "love" ;
cord stri = "detest" ;
strm >> stri;
cout << stri << endl;
render 0 ;
}
The output is:
Any string value that the string object might have had is replaced. If the stringstream is declared above and without any argument, then it is for input or output.
If the string value of the stringstream object (buffer) has spaces, then but the first word will be sent. The sstream member role, str(), has to be used to send the whole string value, including the spaces. Str() tin can exist used to convert a cord literal into the sstream content. It can also be used to return all the content of the stream buffer, including the spaces, to the string object. The following program illustrates this:
#include <cord>
#include <sstream>
#include <iostream>
using namespace std;
int main( )
{
stringstream strm;
strm << "I love her, just he hates her." ;
cord stri;
stri = strm.str ( ) ;
cout << stri << endl;
return 0 ;
}
The output is:
I love her, but he hates her.
Sending Get-go Few Words Into Variables
In the string,
"I dearest her, only he hates her."
If the first 5 words are to be represented by the variables, a, b, c, d, eastward, then these variables tin be made to concord the words. The post-obit code illustrates this:
#include <string>
#include <sstream>
#include <iostream>
using namespace std;
int principal( )
{
stringstream strm;
strm << "I love her, only he hates her." ;
cord a, b, c, d, eastward;
strm >> a >> b >> c >> d >> e;
cout << a << ' ' << b << ' ' << c << ' ' << d << ' ' << e << endl;
return 0 ;
}
The output is:
A unmarried character is a word. Annotation that the comma has been joined with "her". If the comma had been separated from "her", and so it would have been considered equally a divide word.
Counting Number of Words in String Literal
In the previous section, only the first five words were identified. In gild to send all the words to unlike variables, the number of words has to exist known. If the problem is but to know the number of words in the sstream buffer, and then that 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 end of the stream (end-of-file) is reached. The following code illustrates this:
#include <cord>
#include <sstream>
#include <iostream>
using namespace std;
int primary( )
{
stringstream strm;
strm << "I love her, simply he hates her." ;
int counter = 0 ;
string temp;
while ( !strm.eof ( ) ) {
strm >> temp;
counter + = 1 ;
}
cout << counter << endl;
return 0 ;
}
The output is 7. The full stop is fastened to the second "her". Note that the indicator for the finish-of-file is the sstream member function, eof().
Individual Word Frequencies
In the string value,
"I dearest her, and he loves her sister, though he hates her brother."
The word, "her", occurs three times, and the frequency of "her" is indicated to be 3. The discussion, "he", appears ii times, and the frequency of "he" is said to exist ii. The rest of the words have a frequency of i each. The frequency of each word can be determined as follows:
Accept all the words in a C++ map without repetition. Consider the post-obit statement:
where mp is a map object. The first time this statement is encountered in a while-loop, information technology fits in a new central/value pair, whose key is the string word of the variable temp and whose value is one. The next time it is encountered in the same while-loop, with this particular central, no new cardinal/value pair is added to the map. The value of this key/value pair is simply incremented.
And then, the strategy is to have this statement in a while-loop and all the words in the ssstream buffer being read into a temporary variable. And, each value for each key/value pair in the map finally becomes the frequency of the cardinal (word). The following programme illustrates this:
#include <string>
#include <sstream>
#include <map>
#include <iostream>
using namespace std;
int principal( )
{
stringstream strm;
strm << "I love her and he loves her sister, though he hates her blood brother." ;
string temp;
map<cord, int > mp;
while (strm >> temp) {
mp[temp] ++ ;
}
for (map<cord, int > :: iterator it = mp.brainstorm ( ) ; it ! = mp.stop ( ) ; it++ )
cout << it- >outset << " => " << it- >2d << endl;
return 0 ;
}
The output is:
I => 1
and => 1
brother. => 1
hates => one
he => 2
her => three
love => 1
loves => i
sis, => one
though => i
String to Number and Vice-Versa
Cord to Number
To catechumen a string word to a number, but send the cord give-and-take from the sstrream buffer to a variable alleged equally a number. To convert to an int, send the cord word to an int variable. To convert to a float, send the string word to a float variable. The following program illustrates these:
#include <string>
#include <sstream>
#include <iostream>
using namespace std;
int chief( )
{
stringstream strm;
strm << " 30 " ;
int myInt;
strm >> myInt;
int intResult = myInt + 10 ;
cout << intResult << endl;
strm << " 2.5 " ;
float myfloat;
strm >> myfloat;
float fltResult = myfloat + 0.iii ;
cout << fltResult << endl;
render 0 ;
}
The output is:
Number to String
To convert a number to a string give-and-take, only ship the number to the sstream buffer. Then, 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 cord, send the float to the sstream. The following programme illustrates these:
#include <cord>
#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;
bladder myflt = 2.five ;
strm2 << myflt;
string fltStr;
strm2 >> fltStr;
string fltStrRes = fltStr + " yes" ;
cout << fltStrRes << endl;
return 0 ;
}
The output is:
2 stream objects have been used here for the unlike number types.
Note: the sstream buffer consists of characters only.
Conclusion
Stringstream means String Stream. It is also written as sstream. Information technology is a stream. The target for this stream is a cord object. The stream tin utilise 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 discussion frequencies in a string object, and converting a word in text class in the string object to a number, and vice-versa.
Source: https://linuxhint.com/c-stringstream-and-how-to-use-it/
0 Response to "Reading in String From File C++ Stringstream"
Post a Comment