This article turns from http://blog.csdn.net/chavo0/article/details/51038397
Record the two methods used to convert int into string
The first is the to_string function, which is c++11 new, very convenient to use, simple to check: C++11 Standard added global function std::to_string, and std::stoi/stol/ Stoll and so on function (these are string to Int,long, and long Long ~)
To_string This function is still very powerful!
String to_string (int val);
String to_string (long Val);
String to_string (Long long Val);
String to_string (unsigned val);
String to_string (unsigned long Val);
String to_string (unsigned long long Val);
String to_string (float val);
String to_string (double val);
String to_string (Long double val)
Not only int can be converted to string, these can all be oh ~
The second is the use of string flow:
The standard library defines three types of string streams: Istringstream,ostringstream,stringstream
Look at the names. These types are very similar to those in iostream, which can read, write, and read and write string types, and they are indeed derived from the iostream type.
To use them, you need to include the Sstream header file.
In addition to the operations inherited from Iostream
The 1.sstream type defines a constructor that has a string parameter.
That is: StringStream stream (s); A StringStream object that stores s copies is created, S is a string type Object
2. Defines a member named STR that is used to read or set the string value manipulated by the StringStream object:
Stream.str (); Returns the string type object stored in the stream
STREAM.STR (s); Copies the string type S to the stream, returning void
So......
String int_to_string (Int N)
{
Ostringstream stream;
stream<<n; n is an int type
return Stream.str ();
}
Two methods of converting "C + +" int to string (to_string, string flow)