std::string为library type,而int、double为built-in type,两者无法互转。
方法一,使用function template的方式将int转std::string,将double转std:string。
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
![ExpandedBlockStart.gif](https://images.cnblogs.com/OutliningIndicators/ExpandedBlockStart.gif)
#include
<
iostream
>
#include < sstream >
#include < string >
using namespace std;
template < class T >
void convertFromString(T & , const std:: string & );
template < class T >
string ConvertToString(T);
int main() {
string s( " 123 " );
string s1;
// Convert std::string to int、double
int i = 0 ;
convertFromString(i,s);
cout << i << endl;
double d = 0 ;
convertFromString(d,s);
cout << d << endl;
// Convert int、double to std::string
int i1 = 123 ;
s1 = ConvertToString(i1);
cout << s1 << endl;
double d1 = 123.123 ;
s1 = ConvertToString(d1);
cout << s1 << endl;
// stringstream除了基本类型的转换,也支持char *的转换
stringstream stream;
char result[ 8 ] ;
int i2 = 8888 ;
stream << i2; // 向stream中插入8888
stream >> result; // 抽取stream中的值到result
cout << result << endl; // 屏幕显示 "8888"
// 再进行多次转换的时候,必须调用stringstream的成员函数clear()
int second;
stream.clear(); // 在进行多次转换前,必须清除stream
stream << true ; // 插入bool值
stream >> second; // 提取出int
cout << second << endl;
return 0 ;
}
template < class T >
void convertFromString(T & value, const string & s) {
stringstream ss(s);
ss >> value;
}
template < class T >
string ConvertToString(T value) {
stringstream ss;
ss << value;
return ss.str();
}
#include < sstream >
#include < string >
using namespace std;
template < class T >
void convertFromString(T & , const std:: string & );
template < class T >
string ConvertToString(T);
int main() {
string s( " 123 " );
string s1;
// Convert std::string to int、double
int i = 0 ;
convertFromString(i,s);
cout << i << endl;
double d = 0 ;
convertFromString(d,s);
cout << d << endl;
// Convert int、double to std::string
int i1 = 123 ;
s1 = ConvertToString(i1);
cout << s1 << endl;
double d1 = 123.123 ;
s1 = ConvertToString(d1);
cout << s1 << endl;
// stringstream除了基本类型的转换,也支持char *的转换
stringstream stream;
char result[ 8 ] ;
int i2 = 8888 ;
stream << i2; // 向stream中插入8888
stream >> result; // 抽取stream中的值到result
cout << result << endl; // 屏幕显示 "8888"
// 再进行多次转换的时候,必须调用stringstream的成员函数clear()
int second;
stream.clear(); // 在进行多次转换前,必须清除stream
stream << true ; // 插入bool值
stream >> second; // 提取出int
cout << second << endl;
return 0 ;
}
template < class T >
void convertFromString(T & value, const string & s) {
stringstream ss(s);
ss >> value;
}
template < class T >
string ConvertToString(T value) {
stringstream ss;
ss << value;
return ss.str();
}
方法二,先利用c_str()轉成C string,再用atoi()與atof()。
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
![ExpandedBlockStart.gif](https://images.cnblogs.com/OutliningIndicators/ExpandedBlockStart.gif)
#include
<
iostream
>
#include < string >
#include < cstdlib >
using namespace std;
int main() {
string s = " 123 " ;
double n = atof(s.c_str());
// int n = atoi(s.c_str());
cout << n << endl;
}
#include < string >
#include < cstdlib >
using namespace std;
int main() {
string s = " 123 " ;
double n = atof(s.c_str());
// int n = atoi(s.c_str());
cout << n << endl;
}