且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

c++中如何将int转换为字符串

更新时间:2023-02-10 14:31:55

您应该按照以下方式进行操作 -

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;
}