c++中如何将int转换为字符串 [英] How do you convert an int into a string in c++
本文介绍了c++中如何将int转换为字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想将一个 int 转换为一个字符串,以便可以对其进行计算.此代码未按预期工作:
I want to convert an int to a string so can cout it. This code is not working as expected:
for (int i = 1; i<1000000, i++;)
{
cout << "testing: " + i;
}
推荐答案
您应该按照以下方式进行操作 -
You should do this in the following way -
for (int i = 1; i<1000000, i++;)
{
cout << "testing: "<<i<<endl;
}
<<
运算符将负责适当地打印值.
The <<
operator will take care of printing the values appropriately.
如果您还想知道如何将整数转换为字符串,那么下面是使用 字符串流 -
If you still want to know how to convert an integer to string, then the following is the way to do it using the stringstream -
#include <iostream>
#include <sstream>
using namespace std;
int main()
{
int number = 123;
stringstream ss;
ss << number;
cout << ss.str() << endl;
return 0;
}
这篇关于c++中如何将int转换为字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文