C ++ Integer Conversion string example

Source: Internet
Author: User

First case: the output is normal.
# Include <isotream>
# Include <sstream>
# Include <string>
Using namespce STD;
String inttostring (int)
{
Ostringstream ostr;
Ostr String STR = ostr. STR ();
Cout <STR;
Return STR;
}

Case 2: Garbled output
# Include <isotream>
# Include <sstream>
# Include <string>
Using namespce STD;
String inttostring (int)
{
Ostrstream ostr;
Ostr String STR = ostr. STR ();
Cout <STR;
Return STR;
}
========================================================== ========================================
Ostrstream: Derived from ostream (output stream class) and strstreambase (string stream base class)
Istrstream: Derived from istream (input stream class) and strstreambase (string stream base class)
Strstream: Derived from iostream (input/output stream class) and strstreambase (string stream base class)
The istrstream class is used to perform input operations for a C-style stream, that is, an array of strings as the input device.
The ostrstream class is used to perform input operations for a C-style stream, that is, an array of strings as the input device.
The strstream class is used to perform C-style Stream Input and Output operations.
Ostringstream
Istringstream

In C ++, there are two types of string streams. One is defined in sstream and the other is defined in strstream. Their implementations are basically the same.
Strstream includes:
Class strstrambuf;
Class istrstream
Class ostrstream;
Class strstream;
They are written based on the C string char.
Sstream includes:
Class istringstream
Class ostringstream
Class stringbuf
Class stringstream
They are written based on STD: string.
Therefore, ostringstream: STR () returns a string of the STD: string type, and ostrstream: STR () returns a string of the char * type.
Pay attention to the differences between the two. Generally, STD: string is recommended.

Related Article

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.