C + +. "Go" C + + numeric type and string conversion

Source: Internet
Author: User
Tags function prototype



1, C + + numeric type and string conversion-johngu-blog Park. HTML (https://www.cnblogs.com/johngu/p/7878029.html)



2.


1. Convert a numeric type to string1.1 using a function template +ostringstream


Use function templates to convert basic data types (integer, character, Real, Boolean) to String.


// ostringstream object is used for formatted output, commonly used to convert various types to string type // ostringstream only supports << operator template <typenameT> string toString (constT & t) {ostringstream oss; // Create a format The output stream oss << t; // Pass the value as returnoss.str ();} cout << toString (14.2) << endl; // Real-> string: Output 14.2cout << toString (12301 ) <endl; // integer-> string: output 12301cout << toString (123456789785) << endl; // long integer-> string: output 123456789785cout << toString (true) << endl; // boolean -> string: output 1




1.2 using standard library functions std::to_string ()


The STD command space has a C + + standard library function std::to_string () that can be used to convert a numeric type to a string. The Include header file is required for use<string>.



The function prototype is stated as follows:



string to_string (intval);string to_string (longval);string to_string (longlongval);string to_string (unsigned val);string to_string (unsigned longval);string to_string (unsigned longlongval);string to_string (floatval);string to_string (doubleval);string to_string (longdoubleval);




2.string conversion to numeric type 2.1 using function template + Istringstream


StringStream has been introduced in methods that convert int or float types to string types, and can also be used as a type of string to convert to a commonly used numeric type.


#include <iostream> #include <sstream> // The use of stringstream needs to include this header file usingnamespacestd; // Template function: convert string type variables to commonly used numeric types (this method has universal applicability) template <classType> Type stringToNum (conststring & str) {istringstream iss (str); Type num; iss >> num; returnnum;} intmain (intargc, char * argv []) {string str ("00801"); cout << stringToNum <int> (str ) << endl; system ("pause"); return0;}




2.2 Using C standard library functions


The practice is to first convert a string to a char* string, and then convert it to the desired numeric type by the appropriate type conversion function. You need to include standard library functions<stdlib.h>.
(1) string conversion to int32_t


string love = "77"; intilove = atoi (love.c_str ()); / or 16-bit platform converted to long intintilove = strtol (love.c_str (), NULL, 10);


(2) string conversion to uint32_t


// str: String to be converted // endptr: Point to the first non-numeric character after the number in str // base: Conversion base (base), ranging from 2 to 36 unsigned longintstrtoul (constchar * str, char ** endptr, intbase); #Example string love = "77"; unsigned longul; ul = strtoul (love.c_str (), NULL, 10);


(3) string conversion to uint64_t


string love="77";longlongllLove=atoll(love.c_str());


(4) string conversion to uint64_t


UNSIGNED&NBSP;long long int strtoull (const char* str, char** endptr, int base); #示例string love="all"UNSIGNED&NBSP;long long ull;ull = Strtoull (Love.c_str (), NULL, 0);


(5) string converted to float or double


string love="77.77";floatfLove=atof(love.c_str());doubledLove=atof(love.c_str());


(6) String conversion to long double



longdouble strtold (constchar* str, char** endptr);




2.3 using C + + standard library functions


Use the C + + library function introduced by c++11 to convert a string to a numeric type, and the corresponding library function is declared in the header file<string>.


name prototypes Description
Stoi int stoi (const string& str, size_t* idx = 0, int base = 10);
int stoi (const wstring& str, size_t* idx = 0, int base = 10);
Convert string to Integer (function template)
Stol Long STOL (const string& str, size_t* idx = 0, int base = 10);
Long STOL (const wstring& str, size_t* idx = 0, int base = 10);
Convert string to long int (function template)
Stoul unsigned long Stoul (const string& str, size_t* idx = 0, int base = 10);
unsigned long Stoul (const wstring& str, size_t* idx = 0, int base = 10);
Convert string to unsigned integer (function template)
Stoll Long Long Stoll (const string& str, size_t* idx = 0, int base = 10);
Long Long Stoll (const wstring& str, size_t* idx = 0, int base = 10);
Convert string to long long (function template)
Stoull unsigned long long stoull (const string& str, size_t* idx = 0, int base = 10);
unsigned long long stoull (const wstring& str, size_t* idx = 0, int base = 10);
Convert string to unsigned long long (function template)
Stof Float Stof (const string& str, size_t* idx = 0);
Float Stof (const wstring& str, size_t* idx = 0);
Convert string to float (function template)
Stod Double stod (const string& str, size_t* idx = 0);
Double stod (const wstring& str, size_t* idx = 0);
Convert string to double (function template)
Stold Long double stold (const string& str, size_t* idx = 0);
Long double stold (const wstring& str, size_t* idx = 0);
Convert string to long double (function template)



formal parameter description:
STR: The string and wstring versions are overloaded, representing the converted strings.



IDX: Represents a size_t* pointer type, which defaults to a null value. When not empty, the index of the first non-numeric character is obtained when the conversion succeeds. In general, because it is a direct char pointer that subtracts the address and start address values of the last non-numeric character, it also indicates the number of characters that were successfully converted, such as "10" to a value of 10 o'clock, which is*idx2.



Base: Represents the conversion datum, which is 10 binary by default.


Reference documents


[1] C + + Reference
[2]strtoul. C + + Reference
[2]strtoull. C + + Reference
[3]strtold. C + + Reference



3.



4.



5.



C + +. "Go" C + + numeric type and string conversion


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