So far, all of the I/O examples you have seen have been writing to cout or reading from cin. However, there is another set of classes called the stream classes for strings that allow you to use the familiar insertions (<<) and extraction (>>) operators to work with strings. Like istream and ostream, the string streams provide a buffer to hold data. However, unlike cin and cout, these streams are not connected to an I/O channel (such as a keyboard, monitor, etc…). One of the primary uses of string streams is to buffer output for display at a later time, or to process input line-by-line.
There are six string classes for streams: istringstream (derived from istream), ostringstream (derived from ostream), and stringstream (derived from iostream) are used for reading and writing normal characters width strings. wistringstream, wostringstream, and wstringstream are used for reading and writing wide character strings.
There are two ways to get data into a stringstream:
1) Use the insertion (<<) operator:
stringstream os; os << "en garde!" << endl; // insert "en garde!" into the stringstream
2) Use the str(string) function to set the value of the buffer:
stringstream os;
os.str("en garde!"); // set the stringstream buffer to "en garde!"
There are similarly two ways to get data out of a stringstream:
1) Use the str() function to retrieve the results of the buffer:
stringstream os; os << "12345 67.89" << endl; cout << os.str();
This prints:
12345 67.89
2) Use the extraction (>>) operator:
stringstream os; os << "12345 67.89"; // insert a string of numbers into the stream string strValue; os >> strValue; // print the numbers separated by a dash cout << strValue << endl;
This program prints:
12345
Conversion between strings and numbers
Because cheap payday loanbad credit payday loan,bad credit payday loan loan,payday loan fast for bad creditfee loan low paydayonline payday loan instant approvalcheap loan payday,cheap loan long payday termadvance loan online paydayno teletrack no credit check payday loan,check credit loan no paydayapplication loan online paydayadvance cash fax no paydayno faxing needed payday loanadvance cash fast loan paydaypayday loan calculatorcash america payday loan,america cash loan paydayfast loan online payday30 day loan payday,30 day payday loanadvance cash loan payday today,advance cash cash loan loan payday quick,advance cash loan paydayloan military overseas payday,military payday loan,loan military paydaypayday loan store milwaukeeadvance loan payday,advance loan payday wisconsin,advance cash loan loan paydaymilitary payday loaninstant faxless payday loanfaxless loan online paydayfast easy payday loanbest payday loanpayday loan cash advance loanno fax required payday loaneasy payday loanloan payday store,payday loan store in chicago,alabama loan payday storecash loan payday quickcash loan payday,cash advance loan payday internet,instant cash payday loanfast cash advance payday loanadvance cash loan payday softwareadvance advance america cashinheritance cash advancecash advance nowbad credit cash advance,advance bad cash credit services,advance bad cash credit loan paydaycash international loan payday servicescash till payday loancash advance payday loanadvance cash check credit nofirst american cash advanceadvance cash faxing no,1000 advance cash faxing no,no faxing required cash advanceadvance cash loan loan paydayadvance cash lawsuit pre settlement,lawsuit cash advance,lawsuit settlement cash advancecash in advance loan,cash advance loan washington,cash advance loan texaspay day cash advance payday loan,pay day loan cash advancecash loan payday tilladvance america bank cash,advance america cash,advance america cash advanceadvance cash overnightadvance cash day pay,cash advance until pay day the insertion and extraction operators know how to work with all of the basic data types, we can use them in order to convert strings to numbers or vice versa.
First, let’s take a look at converting numbers into a string:
stringstream os; int nValue = 12345; double dValue = 67.89; os << nValue << " " << dValue; string strValue1, strValue2; os >> strValue1 >> strValue2; cout << strValue1 << " " << strValue2 << endl;
This snippet prints:
12345 67.89
Now let’s convert a numerical string to a number:
stringstream os; os << "12345 67.89"; // insert a string of numbers into the stream int nValue; double dValue; os >> nValue >> dValue; cout << nValue << " " << dValue << endl;
This program prints:
12345 67.89
Clearing a stringstream for reuse
There are several ways to empty a stringstream’s buffer.
1) Set it to the empty string using str():
stringstream os;
os << "Hello ";
os.str(""); // erase the buffer
os << "World!";
cout << os.str();
2) Call erase() on the result of str():
stringstream os; os << "Hello "; os.str().erase(); // erase the buffer os << "World!"; cout << os.str();
Both of these programs produce the following result:
World!
When clearing out a stringstream, it is also generally a good idea to call the clear() function:
stringstream os; os << "Hello "; os.str().erase(); // erase the buffer os.clear(); // reset error flags os << "World!"; cout << os.str();
clear() resets any error flags that may have been set and returns the stream back to the ok state. We will talk more about the stream state and error flags in the next lesson.
13.5 — Stream states and input validation
|
Index
|
13.3 — Output with ostream and ios
|
13.5 — Stream states and input validation
Index
13.3 — Output with ostream and ios
No comments yet.