Conversion between C ++ int and string, and between intstring

Source: Internet
Author: User

Conversion between C ++ int and string, and between intstring

1. Convert int to string

  I. to_string Functions

The c ++ 11 standard adds the global function std: to_string:

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 );

Example:

 1 // to_string example   2 #include <iostream>   // std::cout   3 #include <string>     // std::string, std::to_string   4    5 int main ()   6 {   7   std::string pi = "pi is " + std::to_string(3.1415926);   8   std::string perfect = std::to_string(1+2+4+7+14) + " is a perfect number";   9   std::cout << pi << '\n';  10   std::cout << perfect << '\n';  11   return 0;  12 } 13 Output14 pi is 3.141593  15 28 is a perfect number 

  Ii. using string stream

The standard library defines three types of string streams: istringstream, ostringstream, and stringstream. You can see that these types are very similar to those in iostream, you can read, write, and read and write the string types respectively. They are also derived from the iostream type. To use them, you must include the sstream header file.

Except operations inherited from iostream

1. The sstream type defines a constructor with string parameters, namely, stringstream stream (s). It creates a stringstream object that stores s copies, and s is a string object.

2. A member named str is defined to read or set the string value manipulated by the stringstream object: stream. str (); returns the string-type object stream stored in stream. str (s); copy string type s to stream and return void

Example:

1 int aa = 30;2 stringstream ss;3 ss<<aa; 4 string s1 = ss.str();5 cout<<s1<<endl; // 30

2. Convert string to int

  I. Use atoi functions in the standard library. For other types, there are also standard library functions, such as floating point atof () and long atol ().

Example:

1 std::string str = "123";2 int n = atoi(str.c_str());3 cout<<n; //123

  Ii. Use the string Stream object defined in the sstream header file for conversion

1 istringstream is ("12"); // construct the input string stream. The stream content is initialized to a string of 2 int I; 3 is> I; // read an int integer from the is stream and store it in I

 

References:

Http://blog.csdn.net/lxj434368832/article/details/78874108

Https://www.cnblogs.com/aminxu/p/4704281.html

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.