如何最短地(最好是内联方式)将整数转换为字符串?欢迎使用STL和Boost库的答案。
integer = 0123456789101112
。现在,可以通过stringstream
类将此整数转换为字符串。 #include <bits/stdc++.h>
using namespace std;
int main()
{
int n,i;
string s;
stringstream st;
for(i=0;i<=12;i++)
{
st<<i;
}
s=st.str();
cout<<s<<endl;
return 0;
}
#include <string>
#include <stdlib.h>
这里有另一种将整数转换为字符串的简单方法
int n = random(65,90);
std::string str1=(__String::createWithFormat("%c",n)->getCString());
您可以访问此链接以获取更多方法 https://www.geeksforgeeks.org/what-is-the-best-way-in-c-to-convert-a-number-to-a-string/
to_string
,例如:std::string s = std::to_string(42)
。 - user283145