最简单的方法是使用一个字符串流(stringstream):
#include<iostream>
#include<string>
#include<sstream>
using namespace std;
string itos(int i) // 将int 转换成string{
stringstream s;
s << i;
return s.str();
}
int main(){
int i = 127;
string ss = itos(i);
const char* p = ss.c_str();
cout << ss << " " << p << "\n";
}
自然地,这种技术能够将任何使用<<输出的类型转换为字符串。
#include<iostream>
#include<string>
#include<sstream>
using namespace std;
string itos(int i) // 将int 转换成string{
stringstream s;
s << i;
return s.str();
}
int main(){
int i = 127;
string ss = itos(i);
const char* p = ss.c_str();
cout << ss << " " << p << "\n";
}
自然地,这种技术能够将任何使用<<输出的类型转换为字符串。

本文介绍了一种在C++中将整型变量转换为字符串的简单方法,通过使用stringstream来实现这一转换过程。

2476

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



