Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/157.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/string/5.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++ 将int转换为string并添加到c++;_C++_String - Fatal编程技术网

C++ 将int转换为string并添加到c++;

C++ 将int转换为string并添加到c++;,c++,string,C++,String,我想知道如何将int转换为字符串,然后将其添加到existin字符串。i、 e std::string s = "Hello"; //convert 1 to string here //add the string 1 to s 我希望我说的有道理。非常感谢您的回答。如果您要附加的数字是整数或浮点变量,请使用并简单地“添加”它: int some_number=123; std::string some_string=“foo”; some_string+=std::to_string(so

我想知道如何将int转换为字符串,然后将其添加到existin字符串。i、 e

std::string s = "Hello";
//convert 1 to string here
//add the string 1 to s
我希望我说的有道理。非常感谢您的回答。

如果您要附加的数字是整数或浮点变量,请使用并简单地“添加”它:

int some_number=123;
std::string some_string=“foo”;
some_string+=std::to_string(some_编号);
std::cout如果要追加的数字是整数或浮点变量,则使用并简单地“添加”它:

int some_number=123;
std::string some_string=“foo”;
some_string+=std::to_string(some_编号);
std::cout现代的方法是使用
std::to_string(1)
。事实上,对于不同的数字类型,存在不同的
std::to_string
重载

把这些放在一起,你可以把std::string s=“Hello”+std::to_string(1)

或者,您可以使用
std::stringstream
,由于字符串连接操作较少,因此速度更快,而且成本较高:

std::stringstream s;
s << "Hello" << 1;
// s.str() extracts the string
std::strings;
“现代”方法是使用
std::to_string(1)
。事实上,对于不同的数字类型,存在不同的
std::to_string
重载

把这些放在一起,你可以把std::string s=“Hello”+std::to_string(1)

或者,您可以使用
std::stringstream
,由于字符串连接操作较少,因此速度更快,而且成本较高:

std::stringstream s;
s << "Hello" << 1;
// s.str() extracts the string
std::strings;

是的,英语不是我的母语,所以,对不起。我希望新字符串为“Hello1”。
inti=1;s、 附加(std::to_string(i))@Biffen:使用“add”没有什么错(与Java人交谈)。虽然串联是个更好的词。@Bathsheba不,没有错,只是想澄清一下。由于涉及数字,add可能有不同的含义。“add”是指append吗?@Biffen是的,英语不是我的母语,所以,对不起。我希望新字符串为“Hello1”。
inti=1;s、 附加(std::to_string(i))@Biffen:使用“add”没有什么错(与Java人交谈)。虽然串联是个更好的词。@Bathsheba不,没有错,只是想澄清一下。由于涉及数字,add可能有不同的含义。我这样做了,但在输出中只得到一些随机字符。(等等)。(当然还有最初的字符串)@kostataskol那么你又做错了什么。我回答中的密码。如果你没有让它工作,那么创建一个新的问题,并在一个新的问题中显示它,连同可能的输入、预期输出和实际输出。你能相信我实际上没有看到这个部分吗?我想我会的。它起作用了!非常感谢您的帮助。我这样做了,但我只在输出中得到一些随机字符。(等等)。(当然还有最初的字符串)@kostataskol那么你又做错了什么。我回答中的密码。如果你没有让它工作,那么创建一个新的问题,并在一个新的问题中显示它,连同可能的输入、预期输出和实际输出。你能相信我实际上没有看到这个部分吗?我想我会的。它起作用了!非常感谢您的帮助。好的,std::to_字符串(1)完成了。我还尝试了stringstream,它看起来更好,但是我不得不为了一个简单的细节对代码进行太多的更改。无论如何,非常感谢。我真的很感谢你的帮助。好的,std::to_字符串(1)做到了。我还尝试了stringstream,它看起来更好,但是我不得不为了一个简单的细节对代码进行太多的更改。无论如何,非常感谢。我真的很感谢你的帮助。
std::stringstream s;
s << "Hello" << 1;
// s.str() extracts the string