Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/139.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++_Type Conversion_Concatenation - Fatal编程技术网

C++ 如何连接整数并在C+中反向打印+;?

C++ 如何连接整数并在C+中反向打印+;?,c++,type-conversion,concatenation,C++,Type Conversion,Concatenation,我有一段代码,一个循环,其中一个整数变量在每次迭代中得到更新。我希望在将变量的值存储到另一个string类型的变量中之后,一个接一个地添加该变量的值。然后反转字符串,最后在末尾打印 > for example: > > the loop gives an output say 1, 2, 3, 4... and so on.. on each iteration. > then, I want to have a string of va

我有一段代码,一个循环,其中一个整数变量在每次迭代中得到更新。我希望在将变量的值存储到另一个string类型的变量中之后,一个接一个地添加该变量的值。然后反转字符串,最后在末尾打印

>     for example:
>     
>     the loop gives an output say 1, 2, 3, 4... and so on.. on each iteration.
>     then, I want to have a string of value "1234"
>     and then reverse it to finally print "4321"

听起来你很清楚你想要什么。 用谷歌搜索你需要的代码片段,你就会找到

向字符串追加int

如果使用c++11,请使用std::to_string(int变量)将int转换为字符串。否则,请使用stringstream

将int转换为字符串后,只需通过执行以下操作将其与数字一起添加到字符串末尾

std::string str="";
for ...
  str += std::to_string(intVar);
反转字符串

使用std::string,有一个反向函数

std::reverse(str.begin(), str.end());
然后简单地用

printf("%s\n", str.c_str());

您已经尝试过了吗?您需要三个操作:将int转换为字符串,将两个字符串相加,以及反转字符串。将所有3个单独输入谷歌,看看会弹出什么,然后组合。没有尝试过任何特定的东西。每次在循环中都尝试过从整数显式转换为字符串,然后再进行字符串连接,但迄今为止还没有成功。“我希望…”对你有好处!但是你的问题是什么?到目前为止还没有成功-那么你的代码哪里不起作用呢?它返回了什么而不是预期的输出?