1、to_string()
需包含头文件
#include <string>
建议用to_string()
std::string to_string( int value ); //(1) (C++11起)
std::string to_string( long value );//(2) (C++11起)
std::string to_string( long long value );//(3) (C++11起)
std::string to_string( unsigned value );//(4) (C++11起)
std::string to_string( unsigned long value );//(5) (C++11起)
std::string to_string( unsigned long long value );//(6) (C++11起)
std::string to_string( float value );//(7) (C++11起)
std::string to_string( double value );//(8) (C++11起)
std::string to_string( long double value );//(9) (C++11起)
#include <iostream>
#include <string>
int main()
{
double f = 23.43;
std::string f_str = std::to_string(f);
std::cout << f_str << '\n';
}
2、sprintf()函数
#include <iostream>
#include <string>
using namespace std;
int main()
{
int n = 65535;
char t[256];
string s;
sprintf(t,"%d", n); //sprintf(char*,"%d",int)
s = t;
cout << s << endl;
return 0;
}
3、引用
strstream,通过流
#include <iostream>
#include <string>
#include <strstream>
using namespace std;
int main()
{
int n = 65535;
strstream ss;
string s;
ss << n;
ss >> s;
cout << s << endl;
return 0;
}