将int变量转换为字符串 [英] int variables to string
本文介绍了将int变量转换为字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
嗨.
如何将3个整数变量转换为一个字符串?
例如:
Hi.
How to convert 3 integer variable to one string?
Ex:
int a = 1;
int b = 2;
int c = 3;
string str = a" "b" "c" ";//1 2 3
推荐答案
我想应该是:如何编写一个用三个整数表示的字符串?"
您可以出于以下目的使用stringstream
对象:
It should be, I suppose: "How to write a string with the representation of three integers?"
You may use astringstream
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);
这篇关于将int变量转换为字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文