C + + learning-conversion between int and string

Source: Internet
Author: User

In C + +, the conversion of int and string types is encountered.

string-int

First we'll look at two functions:

Atoi

This function converts a char * to an int. Should belong to the standard library function. The following process is required when you want to convert a string to int:

stringcharint

So talented, examples such as the following:

string"1234";int b = atoi(a.c_str());

This is the time to print B 1234 .

Itoa

This function does not seem to belong to the library function when I search, it is function int->char * , so can:

int1234;stringnewstring(itoa(a));

Just have to have itoa function enough.

string int

Here I would like to use the method is stringstream very useful.
Directly above the sample code:

int1234str = ss.str();

This is OK ~ is not very convenient.

It is important to note that the stringstream needs to be emptied before multiple operations. The empty operation is: ss.str("") , no ss.clear() . Remember.

Above.

C + + learning-conversion between int and string

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.