且构网

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

将int变量转换为字符串

更新时间:2023-02-03 09:56:35

我想应该是:如何编写一个用三个整数表示的字符串?"

您可以出于以下目的使用stringstream对象:
It should be, I suppose: "How to write a string with the representation of three integers?"

You may use a stringstream object for the purpose:
#include <string>
#include <sstream>
using namespace std;
int main()
{
  string str;
  stringstream s;
  int i,j,k;
  i = 5; j=-7; k = 1000;
  s << i << " " << j << " " << k  << endl;
  str = s.str();
}


char str[30];
sprintf(str, "%d %d %d", a, b, c);