int-> string
//C++11 支持的方法
int ntime = 800;
//tostring的参数可以是int、double、float、long等等
std::string str = std::to_string(ntime);
//""包起来相当于 const char*,不能使用 + 连接
std::string key = "5" + std::string("&&&") + str;
//不能使用C++11时
#include <sstream>
std::stringstream ss;
std::string str = "";
int ntime = 800;
ss << ntime;
ss >> str;
string -> int
//C++11 支持的方法
std::string str = "800";
int ntime = std::stoi(str);
//其他相关函数有stod、stof、stol、stold、stoll,
//返回值分别为double、float、long、long double、long long
//不能使用C++11时
#include <sstream>
std::stringstream ss;
std::string str = "800";
int ntime = 0;
ss << str;
ss >> ntime;
本文介绍了在C++11及更高版本中,如何使用std::to_string将整型(int)转换为字符串(string),以及使用std::stoi进行反向转换。当不支持C++11时,可以利用stringstream进行类型间的转换。此外,还提到了其他相关函数如stod、stof等用于不同类型的转换。

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



