Istringstream and ostringstream

Source: Internet
Author: User

Istringstream and ostringstream

I used istringstream and ostringstream to write programs. I read others' blog posts and learn from them ~~~~~~.

Iostream Standard Library supports input/output in memory, as long as the stream is bundled with the string object stored in the program memory. In this case, you can use iostream input and output operators to read and write this string object. The standard library defines three types of string streams:
• Istringstream, derived from istream, provides the string READ function.
• Ostringstream, derived from ostream, provides the string writing function.
• Stringstream, derived from iostream, provides the string read/write function.
To use the above class, you must include the sstream header file.
Like fstream, the above types are derived from the iostream type, which means that all operations on iostream apply to the type in sstream. In addition to inherited operations, the sstream type also defines a constructor with string parameters. This constringstream function copies the real parameters of the string type to the stringstream object. The read and write operations on stringstream actually read and write the string object in this object. These classes also define members named 'str' to read or set the string value manipulated by the stringstream object.

Note that although fstream and sstream share the same base class, they do not have other interconnectivity
System. In particular, the stringstream object does not use the open and close functions, while the fstream object
Str is not allowed.
Stringstream-specific operations

Stringstream strm; // create a free stringstream object stringstream strm (s); // create a stringstream object that stores copies of s, where s is a string object strm. str () // return the string type object stored in strm. str (s) // copy string type s to strm and return void

Use and use of stringstream objects 
We have seen a program that processes input in one word or one line at a time. First Process
Use the string input operator in sequence, and use the getline function in the second type. However, some programs require
These two methods must be used at the same time: some operations are implemented based on each row, while others must manipulate each
. It can be implemented using the stringstreams object:

string line, word;      // will hold a line and word from input,respectivelywhile (getline(cin, line)){    // read a line from theinput into line do per-line processing    istringstream stream(line);  // bind to stream to the    line we read    while (stream >> word)    {          // read a word from line         // do per-word processing    }}

Here, the getline function is used to read the entire line from the input. Then, to obtain the words in each row, bind an istringstream object with the row to be read. In this way, you only need to use the regular string input operator to read the words in each row.

Conversion and/or formatting provided by stringstream 
A common usage of stringstream objects is to use this type when automatic formatting is required between multiple data types. For example, if you have a set of numeric data, you need to obtain their string representation, or vice versa. The input and output operations of sstream can automatically convert the arithmetic type to the corresponding string representation, which is also acceptable.

int val1 = 512, val2 = 1024;ostringstream format_message;// ok: converts values to a string representationformat_message << "val1: " << val1 << "\n"<< "val2: " << val2 << "\n"; 

An empty ostringstream object named format_message is created and the specified content is inserted into the object. The focus is that int-type values are automatically converted to equivalent printable strings. Format_message contains the following characters:

val1: 512\nval2: 1024 

Instead, you can use istringstream to read the string object and retrieve the numeric data again. The istringstream object automatically converts the character representation of numeric data to the corresponding arithmetic value.

// str member obtains the string associated with a stringstreamistringstream input_istring(format_message.str());string dump; // place to dump the labels from the formatted message// extracts the stored ascii values, converting back to arithmetic typesinput_istring >> dump >> val1 >> dump >> val2;cout << val1 << " " << val2 << endl;  // prints 512 1024 

Here. The str member obtains the string copy associated with the previously created ostringstream object. Then bind input_istring to string. When reading input_istring, the corresponding values are restored to their original numeric representation. To read input_string, the string object must be divided into several parts. We want numeric data. To get them, we must read (and ignore) the labels around the required data.

Because the input operator reads a value of type, the type of the object to be read must be the same as that of the value read by stringstream. In this example, input_istring is divided into four parts:
The value of the string type is val1, followed by 512, followed by the value of the string type val2, and finally 1024. Generally, when the input operator is used to read a string, the blank space is ignored. Therefore, when reading the string associated with format_message, ignore the linefeed.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.