gpt4 book ai didi

c++ - string::operator+= 问题

转载 作者:塔克拉玛干 更新时间:2023-11-02 23:57:54 24 4
gpt4 key购买 nike

我试图在一个字符串中追加两个字母,但似乎字符串没有改变:

void fun()
{
string str;
str += 'a' + 'b';

cout << str;
}

查看了STL的源码,找到了operator+=的实现,但还是不知道为什么。

basic_string&
operator+=(_CharT __c)
{
this->push_back(__c);
return *this;
}

最佳答案

通过添加 'a' + 'b',您会将 2 个字符加在一起形成另一个字符。然后使用 += 将其添加到字符串中。

此代码将执行您想要的操作:

std::string str;
( str += 'a' ) += 'b';

std::cout << str;

关于c++ - string::operator+= 问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17783653/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com