-
其他类型 -> string
std::string to_string( int value );
std::string to_string( long value );
std::string to_string( long long value );
std::string to_string( unsigned value );
std::string to_string( unsigned long value );
std::string to_string( unsigned long long value );
std::string to_string( float value );
std::string to_string( double value );
std::string to_string( long double value );
例子:
#include <iostream>
#include <string>
int main()
double f = 23.43;
double f2 = 1e-9;
double f3 = 1e40;
double f4 = 1e-40;
double f5 = 123456789;
std::string f_str = std::to_string(f);
std::string f_str2 = std::to_string(f2); // Note: returns "0.000000"
std::string f_str3 = std::to_string(f3); // Note: Does not return "1e+40".
std::string f_str4 = std::to_string(f4); // Note: returns "0.000000"
std::string f_str5 = std::to_string(f5);
std::cout << "std::cout: " << f << '\n'
<< "to_string: " << f_str << "\n\n"
<< "std::cout: " << f2 << '\n'
<< "to_string: " << f_str2 << "\n\n"
<< "std::cout: " << f3 << '\n'
<< "to_string: " << f_str3 << "\n\n"
<< "std::cout: " << f4 << '\n'
<< "to_string: " << f_str4 << "\n\n"
<< "std::cout: " << f5 << '\n'
<< "to_string: " << f_str5 << '\n';
}
-
string -> int
int stoi( const std::string& str, std::size_t* pos = 0, int base = 10 );
int stoi( const std::wstring& str, std::size_t* pos = 0, int base = 10 );
-
string -> long
long stol( const std::string& str, std::size_t* pos = 0, int base = 10 );
long stol( const std::wstring& str, std::size_t* pos = 0, int base = 10 );
-
string -> long long
long long stoll( const std::string& str, std::size_t* pos = 0, int base = 10 );
long long stoll( const std::wstring& str, std::size_t* pos = 0, int base = 10 );
-
string -> unsigned long
unsigned long stoul( const std::string& str, std::size_t* pos = 0, int base = 10 );
unsigned long stoul( const std::wstring& str, std::size_t* pos = 0, int base = 10 );
-
string -> unsigned long long
unsigned long long stoull( const std::string& str, std::size_t* pos = 0, int base = 10 );
unsigned long long stoull( const std::wstring& str, std::size_t* pos = 0, int base = 10 );
-
string -> float
float stof( const std::string& str, std::size_t* pos = 0 );
float stof( const std::wstring& str, std::size_t* pos = 0 );
-
string -> double
double stod( const std::string& str, std::size_t* pos = 0 );
double stod( const std::wstring& str, std::size_t* pos = 0 );
-
string -> long double
long double stold( const std::string& str, std::size_t* pos = 0 );
long double stold( const std::wstring& str, std::size_t* pos = 0 );