In practice, the string series in c ++ -- Conversion between string and integer or floating point

Source: Internet
Author: User
Tags string to number

In practice, the string series in c ++ -- Conversion between string and integer or floating point

It is rarely mentioned in the textbook that the conversion between string and int or float is often encountered in actual projects, especially when displaying UI controls. For example, if you want to display a value in the edit control, you need to convert the value to a string first, and then pay the string to the edit Control.

On the Internet, you will find many conversion methods. I personally think it is best to be concise when efficiency is almost the same.

Here we mainly useStringstreams:

Stringstream is another string-type stream object provided by C ++. It has similar operations with iostream and fstream. To use stringstream, you must first add this line:


Stringstream is mainly used to split a string. You can use clear () and str () to set the specified string to the starting content, and then use> to output a different data, for example:

string s;stringstream ss;int a, b, c;getline(cin, s);ss.clear();ss.str(s);ss >> a >> b >> c;

Let's get down to the truth.
1. number to string in stringstreams
The two steps are as follows:
Output number to stream
Get string from stream

int Number = 123;       string Result;        ostringstream convert;   convert << Number;     Result = convert.str(); 

The preceding code can be reduced to one sentence:

int Number = 123;string String = static_cast
   ( &(ostringstream() << Number) )->str();

It should be noted that number is not limited to int, and float can work the same way.

2. string to number in stringstreams
Two steps are also required:
Construct a stream based on string
Reading value into the variable

string Text = "456"; int Result;          istringstream convert(Text); if ( !(convert >> Result) ) {    Result = 0;             //if that fails set 'Result' to 0}

You can also simplify the preceding code:

string Text = "456";int Number;if ( ! (istringstream(Text) >> Number) ) Number = 0;

3. number string Conversion in C ++ 11
C ++ 11 provides us with a more convenient method:

Convert integer and floating point type to string type
Std: to_string
The reload is as follows:
String to_string (int val );
String to_string (long val );
String to_string (long val );
String to_string (unsigned val );
String to_string (unsigned long val );
String to_string (unsigned long val );
String to_string (float val );
String to_string (double val );
String to_string (long double val );

String to integer:
Stoi, stol, stoll

Character string to floating point type:
Stof, stod, stold

int number = 123;string text = to_string(number);text = "456"number = stoi(number);

4. string and number conversion in C-stdio
Number to String

int Number = 123; char Result[16]; sprintf ( Result, "%d", Number ); 

String to Number

char Text[] = "456"; int Result; sscanf ( Text, "%d", &Result );

5. string and number conversion in C-stdlib
However, it should be noted that the above functions are not a standard and should be used as little as possible.

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: 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.