Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/variables/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 如何从C++;?_C++_Variables_Cout - Fatal编程技术网

C++ 如何从C++;?

C++ 如何从C++;?,c++,variables,cout,C++,Variables,Cout,我有4个变量,叫做:number,x,y,z x = 1, y = 2, z = 3 我希望变量数能够取x、y和z的值,并且能够显示123的值 到目前为止,我已经尝试过: number = x + y + z; but the answer is 6. 或 number=x您可以使用将int转换为string,然后+运算符充当串联操作 #include <string> std::string concatenated = std::to_string(x) + std::to

我有4个变量,叫做:number,x,y,z

x = 1,
y = 2,
z = 3
我希望变量数能够取x、y和z的值,并且能够显示123的值

到目前为止,我已经尝试过:

number = x + y + z; but the answer is 6.

number=x您可以使用将
int
转换为
string
,然后
+
运算符充当串联操作

#include <string>

std::string concatenated = std::to_string(x) + std::to_string(y) + std::to_string(z);
std::cout << concatenated;
#包括
std::string concatenated=std::to_string(x)+std::to_string(y)+std::to_string(z);
C++中的std::cout:

cout << x << y << z
或者将它们放在一个字符串中:

ostringstream convert;   // stream used for the conversion

convert << x << y << z; 

std::string result = convert.str(); 
ostringstream转换;//用于转换的流

convert听起来像是要将字符转换为字符串,执行字符串加法,然后再转换回数字

此链接应有助于:
[

你有很多方法可以得到你的结果:

  • 数字:
    number=x*100+y*10+z
  • 字符串连接,如图所示
  • 直接使用字符:

    char resul[4];
    resul[0] = '0' + x;
    resul[1] = '0' + y;
    resul[2] = '0' + z;
    resul[3] = '\0';
    

可能还有很多其他的…

这些变量是什么类型的?很难说他什么时候说要“显示”。我会更新我的答案
ostringstream convert;   // stream used for the conversion

convert << x << y << z; 

std::string result = convert.str(); 
char resul[4];
resul[0] = '0' + x;
resul[1] = '0' + y;
resul[2] = '0' + z;
resul[3] = '\0';