将int附加到std :: string [英] Append int to std::string

查看:102
本文介绍了将int附加到std :: string的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试了两种不同的方式将 int 附加到 std :: string ,令我惊讶的是,我得到了不同的结果:

I tried two different ways to append an int to a std::string, and to my surprise, I got different results:

#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不要以为问题就像如何连接std :: string和int吗?

I don't think the question is like How to concatenate a std::string and an int?

在这个问题中,没有答案使用 + = 运算符。在 std :: string + =
+ 运算符之间c $ c>是解决我的疑问的关键。

In that question,no answer uses += operator.And the difference between += and + operator of std::string is the key to solve my doubt.

坦率地说,这个问题是解释为什么c ++如此难以掌握的一个很好的例子。

Frankly,the question is a good example for explaining why c++ is so difficult to master.

推荐答案

TL; DR operator + = 是类成员函数在类字符串中,而 operator + 是模板函数。

TL;DR operator+= is a class member function in class string, while operator+ is a template function.

标准类 template< typename CharT> basic_string< CharT> 具有重载的函数 basic_string&运算符+ =(CharT),而字符串只是 basic_string< char>

The standard class template<typename CharT> basic_string<CharT> has overloaded function basic_string& operator+=(CharT), and string is just basic_string<char>.

由于适合较低类型的值可以自动转换为该类型,因此在表达式 s + = 2 中,将2视为 int ,但改为 char 。它与 s + =‘\x02’具有完全相同的效果。附加了ASCII码为2(STX)的字符,不是字符'2'(ASCII值为50或0x32)。

As values that fits in a lower type can be automatically cast into that type, in expression s += 2, the 2 is not treated as int, but char instead. It has exactly the same effect as s += '\x02'. A char with ASCII code 2 (STX) is appended, not the character '2' (with ASCII value 50, or 0x32).

但是,string没有像 string operator +(int) s + 2 这样的重载成员函数无效表达式,从而在编译期间引发错误。 (以下更多内容)

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)

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

You can use operator+ function in string in these ways:

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






人们担心为什么2不能通过 operator +

template <typename CharT>
  basic_string<CharT>
  operator+(const basic_string<CharT>& lhs, CharT rhs);

上面是<中的plus运算符的原型 [note] code> s + 2 ,并且由于它是模板函数,因此需要同时实现 operator +< char> operator +< int> ,这是有冲突的。有关详细信息,请参见为什么不将自动向下转换应用于模板功能?

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?

同时, operator + = 的原型是:

template <typename CharT>
class basic_string{
    basic_string&
      operator+=(CharT _c);
};

您看到的是,这里没有模板(这是一个类成员函数),因此编译器推断出CharT类型是来自类实现的 char ,并且 int(2)自动转换为 char(2 )

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).

注意:从C ++标准包含源复制时,会删除不必要的代码。其中包括用于模板类 basic_string的类型名称2和3(Traits和Allocator)以及不必要的下划线,以提高可读性。

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.

这篇关于将int附加到std :: string的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆