#include <iostream>
#include <sstream>
using namespace std;
int main(int argc, char *argv[])
{
//使用C标准库里面的atoi
//字符串转数字
string str = "123";
int value = std::atoi(str.c_str());
cout << "string to int value:" << value << endl;
//C++标准库里面,使用stringstream
std::stringstream sstr;
sstr << "520";
int value2;
sstr >> value2;
cout<<"string to int value2:" << value2 << endl;
//数字转字符串
char str[10];
int a = 1234321;
sprintf(str,"%d",a);
cout << str << endl;
int value3 = 125;
std::stringstream sstr2;
sstr2 << value3;
string str2;
sstr2 >> str2;
cout<<"int to string str2:" << str2 << endl;
return 0;
}
stringstream sTmp;
sTmp << iJsValue["LocationID"].asInt();
string sLocationID = sTmp.str();//Int to String
const char *cLocationID = sLocationID.data();//String to char*
//关于s.c_str()
//不宜使用
char* c;
string s="1234";
c = s.c_str(); //c最后指向的内容是垃圾,因为s对象被析构,其内容被处理
//正确使用
char c[20];
string s="1234";
strcpy(c,s.c_str());
数字与字符串互转
最新推荐文章于 2023-10-17 09:07:06 发布
392

被折叠的 条评论
为什么被折叠?



