1. Convert numeric type to string
1.1 using Function Templates +ostringstream
Use function templates to convert basic data types (integer, character, solid, Boolean) to String.
The output that the Ostringstream object uses for formatting, often used to convert various types to string types
//ostringstream only support the << operator
Template<typename T > String toString (const t& T) {
ostringstream oss; Create a formatted output stream
oss<<t; Pass the value in a stream of return
oss.str ();
}
Cout<<tostring (14.2) <<endl; Real-->string: Output 14.2
cout<<tostring (12301) <<endl;//integral->string: Output 12301 cout<<
ToString (123456789785) <<endl;//long->string: Output 123456789785
cout<<tostring (True) <<endl ; Boolean->string: Output 1
1.2 using standard library functions std::to_string ()
There is a C + + standard library function std::to_string () in the STD command space that can be used to convert numeric types to string. The Include header file <string> is required for use.
The function prototype is stated as follows:
String to_string (int val);
String to_string (long val);
String to_string (Long long Val);
String to_string (unsigned val);
String 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);
2.string conversion to numeric type
2.1 using Function templates + istringstream
StringStream has been described in methods of converting int or float to string types, and can also be used to convert a string type to a commonly used numeric type.
#include <iostream>
#include <sstream> //Use stringstream need to introduce this header file
using namespace std;
Template functions: Converts a String type variable to a commonly used numeric type (this method is universally applicable)
template <class type>
Type stringtonum (const string& STR ) {
istringstream iss (str);
Type num;
ISS >> num;
return num;
}
int main (int argc, char* argv[]) {
string str ("00801");
cout << stringtonum<int> (str) << Endl;
System ("pause");
return 0;
}
2.2 using the C standard library function
The practice is to convert a string to a char* string, and then convert it to the desired numeric type by using the corresponding type conversion function. Need to include standard library functions <stdlib.h>.
(1) string converted to int32_t
String Love= "a";
int Ilove=atoi (LOVE.C_STR ());
or 16-bit platform conversion to long int
int ilove=strtol (LOVE.C_STR (), null,10);
(2) string converted to uint32_t
Str: to be converted string
//endptr: The first non-numeric character after the number in str
//base: Conversion cardinality (in), range from 2 to
unsigned long int strtoul (const char* STR, char** endptr, int base);
#示例
string love= ";
" unsigned long ul;
UL = Strtoul (Love.c_str (), NULL, 10);
(3) string converted to int64_t
String Love= "a";
Long Long Lllove=atoll (LOVE.C_STR ());
(4) string converted to uint64_t
unsigned long long int strtoull (const char* STR, char** endptr, int base);
#示例
string love= ";
" unsigned long long ull;
ull = Strtoull (Love.c_str (), NULL, 0);
(5) string converted to float or double
String love= "77.77";
Float Flove=atof (Love.c_str ());
Double Dlove=atof (Love.c_str ());
(6) string to long double
Long double strtold (const char* STR, char** endptr);
2.3 using C + + standard library functions
The C + + library function introduced with c++11 converts a string to a numeric type, and the corresponding library function is declared in 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: overloaded string and wstring versions, representing the converted strings.
IDX: Represents a size_t* pointer type, and the default is a null value. is not empty, gets the subscript of the first non-numeric character 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" when the success was 10 o'clock and the value of *IDX was 2.
Base: Represents the Conversion datum, the default is 10. Reference Documents
[1] C + + Reference
[2]strtoul. C + + Reference
[2]strtoull. C + + Reference
[3]strtold. C + + Reference