1、不安全的方式
整数转字符串
#include<string>
int nA = 1;
string sB;
sB = std::to_string(nA);
整数转字符串
int nA;
string sB = "1";
nA = std::atoi(sB);
2、安全的方式(通过流转换)
#include<iostream>
template<typename out_type, typename in_value>
//支持任意类型之间的互相转换
out_type convert(const in_value & t){
std::stringstream stream;
stream << t;//向流中传值
out_type result;//这里存储转换结果
stream >> result;//向result中写入值
return result;
}
使用示例:
整数转字符串
int nA = 1;
string sB;
sB = convert<string,int>(nA);
整数转字符串
int nA;
string sB = "1";
sB = convert<int,string>(sB);
其他:
- 字符数组转string
参考
https://blog.csdn.net/qq_35937273/article/details/82529117