C++ 将 int 附加到 std::string
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/45505477/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Append int to std::string
提问by navylover
I tried two different ways to append an int
to a std::string
, and to my surprise, I got different results:
我尝试了两种不同的方法将 an 附加int
到 a std::string
,令我惊讶的是,我得到了不同的结果:
#include <string>
int main()
{
std::string s;
s += 2; // compiles correctly
s = s + 2; // compiler error
return 0;
}
Why does it compile and work correctly when I use the +=
operator, but fail when I use the +
operator?
为什么当我使用+=
运算符时它可以编译并正常工作,但使用+
运算符时却失败?
I don't think the question is like How to concatenate a std::string and an int?
我不认为问题类似于如何连接 std::string 和 int?
In that question,no answer uses +=
operator.And the difference between +=
and +
operator of std::string
is the key to solve my doubt.
在那个问题中,没有答案使用+=
运算符。+=
和+
运算符之间的区别std::string
是解决我疑问的关键。
Frankly,the question is a good example for explaining why c++ is so difficult to master.
坦率地说,这个问题是解释为什么 c++ 如此难以掌握的一个很好的例子。
回答by iBug
TL;DRoperator+=
is a class member function in class string
, while operator+
is a template function.
TL;DRoperator+=
是 中的类成员函数class string
,而operator+
是模板函数。
The standard class template<typename CharT> basic_string<CharT>
has overloaded function basic_string& operator+=(CharT)
, and string is just basic_string<char>
.
标准类template<typename CharT> basic_string<CharT>
具有重载函数basic_string& operator+=(CharT)
,而字符串只是basic_string<char>
.
As values that fits in a lower type can be automatically cast into that type, in expression s += 2
, the 2 is nottreated as int
, but char
instead. It has exactlythe same effect as s += '\x02'
. A char with ASCII code 2 (STX) is appended, notthe character '2' (with ASCII value 50, or 0x32).
由于适合较低类型的值可以自动转换为该类型,因此在 expression 中s += 2
, 2不被视为int
,char
而是。它正好相同的效果s += '\x02'
。附加了 ASCII 代码 2 (STX)的字符,而不是字符“2”(ASCII 值为 50 或 0x32)。
However, string does not have an overloaded member function like string operator+(int)
, s + 2
is not a valid expression, thus throws an error during compilation. (More below)
但是,string 没有像 那样的重载成员函数string operator+(int)
,s + 2
它不是有效的表达式,因此在编译期间会引发错误。(更多见下文)
You can use operator+ function in string in these ways:
您可以通过以下方式在字符串中使用 operator+ 函数:
s = s + char(2); // or (char)2
s = s + std::string(2);
s = s + std::to_string(2); // C++11 and above only
For people concerned about why 2 isn't automatically cast to char
with operator+
,
对于担心为什么 2 不会自动转换为char
with 的人operator+
,
template <typename CharT>
basic_string<CharT>
operator+(const basic_string<CharT>& lhs, CharT rhs);
The above is the prototype[note]for the plus operator in s + 2
, and because it's a template function, it is requiring an implementation of both operator+<char>
and operator+<int>
, which is conflicting. For details, see Why isn't automatic downcasting applied to template functions?
上面是加号操作符 in的原型[注]s + 2
,因为它是一个模板函数,所以需要同时实现operator+<char>
and operator+<int>
,这是冲突的。有关详细信息,请参阅为什么不将自动向下转换应用于模板函数?
Meanwhile, the prototype of operator+=
is:
同时,原型operator+=
为:
template <typename CharT>
class basic_string{
basic_string&
operator+=(CharT _c);
};
You see, no template here (it's a class member function), so the compiler deduces that type CharT is char
from class implementation, and int(2)
is automatically cast into char(2)
.
你看,这里没有模板(它是一个类成员函数),所以编译器推断出 CharT 类型char
来自类实现,并int(2)
自动转换为char(2)
.
Note:Unnecessary code is stripped when copying from C++ standard include source. That includes typename 2 and 3 (Traits and Allocator) for template class "basic_string", and unnecessary underscores, in order to improve readability.
注意:从 C++ 标准包含源复制时,会删除不必要的代码。这包括模板类“basic_string”的 typename 2 和 3(特征和分配器),以及不必要的下划线,以提高可读性。
回答by Bathsheba
s += 2;
is not doing what you think it's doing. It calls the overloaded +=
operator to a char
. It does notappend the character'2'
, but rather the character with value2, and the result will depend on the encodingused on your platform.
s += 2;
不是在做你认为它在做的事情。它将重载+=
运算符调用到 a char
。它并没有追加字符'2'
用,而是字符值2,结果将取决于编码的平台上使用。
There is no operator overload defined to allow s + 2
to compile1. Hence the error.
没有定义运算符重载来允许s + 2
编译1。因此错误。
The solution in both cases is to use std::to_string(2)
rather than the int
literal 2.
在这两种情况下的解决方案是使用std::to_string(2)
而不是int
文字 2。
1Essentially the reason is because operator+=
is not a template function, but std::operator+
is, and overload resolution will favour a non-template function over a template one.
1本质上的原因是因为operator+=
不是模板函数,而是模板函数,std::operator+
并且重载决议将有利于非模板函数而不是模板函数。
回答by Cory Kramer
The correct way to add to your string
would be
添加到您的正确方法string
是
std::string s;
s += std::to_string(2);
s = s + std::to_string(2);
回答by Xatyrian
While @CoryKramer answer gives you the correct way to add an integer to a string, it doesn't explain why the instruction s = s + 2
does not compile.
虽然@CoryKramer 的答案为您提供了向字符串添加整数的正确方法,但它并没有解释为什么指令s = s + 2
无法编译。
The difference between the two instruction is that in the first one you use the std::string
's += operatorwhile in the second instruction, the compiler tries to cast 2
to a string.
两条指令之间的区别在于,在第一条指令中使用std::string
's += 运算符,而在第二条指令中,编译器尝试转换2
为字符串。
There is no implicit conversion between int
and std::string
. however, you can cast an int
to char
, so this is why s += 2
works.
int
和之间没有隐式转换std::string
。但是,您可以强制转换为int
to char
,所以这就是有效的原因s += 2
。