Two methods of converting "C + +" int to string (to_string, string flow)

Source: Internet
Author: User

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)

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.