Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/160.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追加到std::string_C++_Stdstring - Fatal编程技术网

C++ 将int追加到std::string

C++ 将int追加到std::string,c++,stdstring,C++,Stdstring,我尝试了两种不同的方法将int附加到std::string,但出乎意料的是,我得到了不同的结果: #include <string> int main() { std::string s; s += 2; // compiles correctly s = s + 2; // compiler error return 0; } #包括 int main() { std::字符串s; s+=2;//编译正确 s=s+2;//编译器错误

我尝试了两种不同的方法将
int
附加到
std::string
,但出乎意料的是,我得到了不同的结果:

#include <string>

int main()
{
    std::string s;
    s += 2;     // compiles correctly
    s = s + 2;  // compiler error

    return 0;
}
#包括
int main()
{
std::字符串s;
s+=2;//编译正确
s=s+2;//编译器错误
返回0;
}
为什么当我使用
+=
操作符时它能正确编译和工作,而当我使用
+
操作符时它却失败了

我不认为这个问题是

在这个问题中,没有答案使用
+=
运算符。而
+=
+
操作符的
std::string
是解决我疑问的关键


P>坦白地说,这个问题是解释C++为什么如此难掌握的一个很好的例子。

< P>正确的添加到<代码>字符串 >的方式是

std::string s;
s += std::to_string(2);
s = s + std::to_string(2);

s+=2没有做您认为它正在做的事情。它将重载的
+=
运算符调用为
字符。它不会附加字符
'2'
,而是附加值为2的字符,结果取决于平台上使用的编码

没有定义允许编译
s+2
的运算符重载1。因此出现了错误

两种情况下的解决方案都是使用
std::to_string(2)
而不是
int
literal 2



1本质上,原因是
运算符+=
不是模板函数,但
std::operator+
是,重载解析将偏向于非模板函数而非模板函数。

而@CoryKramer answer为您提供了向字符串添加整数的正确方法,它没有解释为什么指令
s=s+2
没有编译

这两条指令之间的区别在于,在第一条指令中使用
std::string
,而在第二条指令中,编译器尝试将
2
转换为字符串


int
std::string
之间没有隐式转换。但是,您可以将
int
强制转换为
char
,这就是
s+=2
起作用的原因;DR
运算符+=
类字符串
中的类成员函数,而
运算符+
是模板函数

标准类
template basic\u string
具有重载函数
basic\u string&operator+=(图表)
,而string只是
basic\u string

由于适合较低类型的值可以自动转换为该类型,因此在表达式
s+=2
中,2不会被视为
int
,而是被视为
char
。它的效果与
s+='\x02'
完全相同。将带有ASCII代码2(STX)的字符追加到字符“2”(ASCII值为50或0x32)上

但是,字符串没有像
string operator+(int)
这样的重载成员函数,
s+2
不是有效的表达式,因此在编译过程中会抛出错误。(详见下文)

您可以通过以下方式在字符串中使用运算符+函数:

s = s + char(2); // or (char)2
s = s + std::string(2);
s = s + std::to_string(2); // C++11 and above only

对于那些关心为什么2不能通过
操作符+
自动转换为
字符的人

template <typename CharT>
  basic_string<CharT>
  operator+(const basic_string<CharT>& lhs, CharT rhs);
你看,这里没有模板(它是一个类成员函数),因此编译器从类实现中推断类型图是
char
,而
int(2)
自动转换为
char(2)



注意:从C++标准中复制源代码时,不必要的代码会被剥离。这包括模板类“basic_string”的typename 2和3(Traits和Allocator)以及不必要的下划线,以提高可读性。

注意,这仅在C++11及更高版本上可用。@AndreKampling 2017年。除非问题被特别标记或以其他方式提到C++03,否则可以假定C++11支持可用。通常推荐的将对象输出到字符串的方法是使用std::ostringstream,将对象流式传输到该字符串,然后使用.str()函数从std::ostringstream获取std::字符串。这应该适用于所有C++语言,以及所有具有插入操作符的对象。为什么会这样?函数
std::to_string
在这种情况下是完全合理的,并且就是为了这个目的而创建的。在这两种情况下,您都没有添加整数
2
的字符串表示形式。您想使用
std::to_string
。我不确定,但我认为
操作符+
期望的是
字符串
而不是
int
您不能将
int
附加到字符串。您可以做的是将表示整数值的文本附加到字符串中。不要忽略转换步骤。可能重复的
s=s+2
不会崩溃。它无法编译。
s+2
不编译,但
s+'\x02'
编译。你怎么解释?@iBug我相信它与“\x02”是一个字符有关,+运算符知道如何将字符添加到字符串中,因为字符串本质上是一个奇特的字符数组。但是我不明白为什么编译器调用
string&operator+=(char c)但不是
字符串运算符+(常量字符串和lhs,字符rhs)?我认为这本身就是一个(好的)问题(这与编译器必须尝试将2转换为
std::string
这一事实有关,但它不能)。为什么不去问呢?@AndreKampling:我屈服了,补充了更多的细节。
s+=2
编译的事实真的感觉像是一个缺陷。也可以看看这个问题。为什么表达式
s+2
中的2不是作为
char
处理的,而是在
s+=2
中处理的?@AndreKampling:这与事实有关+=是非模板函数,但+是模板函数。
template <typename CharT>
class basic_string{
    basic_string&
      operator+=(CharT _c);
};