std::string与int、double相互转换
2010-05-18 16:56
459 查看
std::string为library type,而int、double为built-in type,两者无法互转。
方法一,使用function template的方式将int转std::string,将double转std:string。
代码
#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;
}
方法一,使用function template的方式将int转std::string,将double转std:string。
代码
#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;
}
相关文章推荐
- Java语法练习:String和Int、Double、Boolean的相互转换
- std::string 和 int之间的相互转换
- [C++] string与int, float, double相互转换
- Java中String类型,int类型,double类型相互转换
- VC 中 Cstring 与 int、float、double、char、string数据类型的相互转换
- String与int、long、float、double等相互转换
- VC 中 Cstring 与 int、float、double、char、string数据类型的相互转换
- c++ int,double等数字类型与string的相互转换
- C++中 char*, string, CString, int, double之间相互的转换
- String、CString、char*、int、float、double相互转换
- C++中string,char,int,double等数据类型的相互转换及与ASCII码的转换
- Swift入门(九)——String与Int、Double、Float等数字相互转换
- String与Int、Double、Float等数字相互转换
- String数据类型转换成long、int、double、float、boolean、char等七种数据类型
- Android中 int,float,Double,String 互相转换
- c++ CString string char* char int类型的相互转换
- (原創) 如何将int,double转std::string? (C/C++) (template)
- 实战c++中的string系列--std:vector<char> 和std:string相互转换(vector to stringstream)
- int 与 String 类相互转换方法
- CString,string,char[],int相互之间的转换