Conversion of strings to numbers in C + + 11 features

Source: Internet
Author: User

When writing C + +, you often encounter the conversion of strings to numbers such as string and int, which can be easily converted through functions such as parseint and valueof in an integer and string wrapper class in a language that is particularly rich in Java, while in C + + There has been no particularly good way to deal with it.

In my previous experience, I was converting through the string stream StringStream in C + +, through the following functions:

1 int str2int (string  input) {2        stringstream SS; 3         ss<<input; 4         int Res; 5         ss>>Res; 6         return Res; 7     }

And:

1     string int2str (int2        stringstream SS; 3         ss<<input; 4         string Res; 5         ss>>Res; 6         return Res; 7     }

However, the efficiency of this writing is not high, especially in the case of high-volume conversion needs to be done.

Until today, when searching further online, we found that the string class has added a full processing method to the C++11 added feature:

std::to_string
String to_string (int val); string to_string (long val); string to_string (Long long Val); string to_string (unsigned val); St  Ring 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);

The to_string function converts almost all numeric types to strings, and functions such as std::stod,std::stof and so on can easily convert a string into a variety of numeric types, as seen here.
Std::stoi
NT Stoi (const string&  str, size_t* idx = 0, int base = ten), int stoi (const wstring& str, size_t* idx = 0, int base = 10);
The following is an analysis of the meaning of two optional parameters in the Stoi function: Parameters
STR//The first parameter is a string that needs to be converted to a number
String object with the representation of a integral number.
IDX//The second parameter is a pointer to a size_t type variable that can be passed in to get the starting position of the string after it is converted to a number, depending on the example
Pointer to a object of type size_t, whose value is set by the function to position of the next character in s TR after the numerical value.
This parameter can also was a null pointer, in which case it's not used.
The base///third parameter represents the representation of a number in a string, usually binary, decimal (default), 16, and so on.
numerical Base (radix) that determines the valid characters and their interpretation.
If This is 0, the base used are determined by the format of the sequence (see Strtol for details). Notice The default this argument is 0.
1 //Stoi Example2#include <iostream>//Std::cout3#include <string>//std::string, Std::stoi4 5 intMain ()6 {7STD::stringStr_dec ="2001, A Space Odyssey";8STD::stringStr_hex ="40c3";9STD::stringStr_bin ="-10010110001";TenSTD::stringStr_auto ="0x7f"; One  ASTD::string:: Size_type sz;//alias of size_t -  -   intI_dec = Std::stoi (str_dec,&sz); SZ's pointer passes as a parameter to the function, obtaining the starting position after truncating the number the   intI_hex = Std::stoi (Str_hex,nullptr, -); -   intI_bin = Std::stoi (Str_bin,nullptr,2); -   intI_auto = Std::stoi (Str_auto,nullptr,0); -  +Std::cout << Str_dec <<": "<< I_dec <<"and ["<< str_dec.substr (SZ) <<"]\n"; Substr start interception from the truncation point -Std::cout << Str_hex <<": "<< I_hex <<'\ n'; +Std::cout << Str_bin <<": "<< I_bin <<'\ n'; AStd::cout << Str_auto <<": "<< I_auto <<'\ n'; at  -   return 0; -}

Output:

2001, A Space Odyssey: 2001 and [, A Space Odyssey]40c3:  16579-10010110001: -12010x7f: 127

Reference:

http://www.cplusplus.com/reference/string/stoi/

http://www.cplusplus.com/reference/string/to_string/

Conversion of strings to numbers in C + + 11 features

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.