std::operator+(std::basic_string)
在标头 <string> 定义 | ||
template<class CharT, class Traits, class Alloc > std::basic_string<CharT,Traits,Alloc> | (1) | (C++20 起为 constexpr ) |
template<class CharT, class Traits, class Alloc > std::basic_string<CharT,Traits,Alloc> | (2) | (C++20 起为 constexpr ) |
template<class CharT, class Traits, class Alloc > std::basic_string<CharT,Traits,Alloc> | (3) | (C++20 起为 constexpr ) |
template<class CharT, class Traits, class Alloc > constexprstd::basic_string<CharT,Traits,Alloc> | (4) | (C++26 起) |
template<class CharT, class Traits, class Alloc > std::basic_string<CharT,Traits,Alloc> | (5) | (C++20 起为 constexpr ) |
template<class CharT, class Traits, class Alloc > std::basic_string<CharT,Traits,Alloc> | (6) | (C++20 起为 constexpr ) |
template<class CharT, class Traits, class Alloc > constexprstd::basic_string<CharT,Traits,Alloc> | (7) | (C++26 起) |
template<class CharT, class Traits, class Alloc > std::basic_string<CharT,Traits,Alloc> | (8) | (C++11 起) (C++20 起为 constexpr ) |
template<class CharT, class Traits, class Alloc > std::basic_string<CharT,Traits,Alloc> | (9) | (C++11 起) (C++20 起为 constexpr ) |
template<class CharT, class Traits, class Alloc > std::basic_string<CharT,Traits,Alloc> | (10) | (C++11 起) (C++20 起为 constexpr ) |
template<class CharT, class Traits, class Alloc > std::basic_string<CharT,Traits,Alloc> | (11) | (C++11 起) (C++20 起为 constexpr ) |
template<class CharT, class Traits, class Alloc > constexprstd::basic_string<CharT,Traits,Alloc> | (12) | (C++26 起) |
template<class CharT, class Traits, class Alloc > std::basic_string<CharT,Traits,Alloc> | (13) | (C++11 起) (C++20 起为 constexpr ) |
template<class CharT, class Traits, class Alloc > std::basic_string<CharT,Traits,Alloc> | (14) | (C++11 起) (C++20 起为 constexpr ) |
template<class CharT, class Traits, class Alloc > std::basic_string<CharT,Traits,Alloc> | (15) | (C++11 起) (C++20 起为 constexpr ) |
template<class CharT, class Traits, class Alloc > constexprstd::basic_string<CharT,Traits,Alloc> | (16) | (C++26 起) |
返回含有来自 lhs 的字符后随来自 rhs 的字符的字符串。等价于:
结果所用的分配器为: 1-4)std::allocator_traits<Alloc>::select_on_container_copy_construction(lhs.get_allocator()) 5-7)std::allocator_traits<Alloc>::select_on_container_copy_construction(rhs.get_allocator()) 8-12)lhs.get_allocator() 13-16)rhs.get_allocator() 换言之:
每种情况下,当两者是拥有同一值类别的 (8-16) 将所有右值 | (C++11 起) |
目录 |
[编辑]参数
lhs | - | 字符串、字符串视图(C++26 起)、字符或指向空终止字符序列首字符的指针 |
rhs | - | 字符串、字符串视图(C++26 起)、字符或指向空终止字符序列首字符的指针 |
[编辑]返回值
含有来自 lhs 的字符后随来自 rhs 的字符的字符串,使用如上确定的分配器(C++11 起)。
注解涉及有状态分配器时(例如用 std::pmr::string 时)(C++17 起),应该谨慎使用
using my_string =std::basic_string<char, std::char_traits<char>, my_allocator<char>>; my_string cat();const my_string& dog(); my_string meow =/* ... */, woof =/* ... */; meow + cat()+/*...*/;// 使用 meow 的分配器上的 select_on_container_copy_construction woof + dog()+/*...*/;// 转而使用 dog() 的返回值的分配器 meow + woof + meow;// 使用 meow 的分配器上的 SOCCC meow +(woof + meow);// 转而使用 woof 的分配器上的 select_on_container_copy_construction 对于 // 令最终结果使用 my_favorite_allocator my_string(my_favorite_allocator)+ meow + woof + cat()+ dog(); 为了更好且可移植地对分配器进行控制,应该在以所欲分配器构造的结果字符串上,使用 | (C++11 起) |
根据重载决议规则,使用 std::type_identity_t 作为重载 (4)、(7)、(12) 和 (16) 的形参,保证了 std::basic_string<CharT, Traits, Allocator> 类型的对象总是可以与能够隐式转换为 std::basic_string_view<CharT, Traits> 的
| (C++26 起) |
[编辑]示例
#include <iostream>#include <string>#include <string_view> int main(){std::string s1 ="Hello";std::string s2 ="world";constchar* end ="!\n";std::cout<< s1 +' '+ s2 + end; std::string_view water{" Water"};#if __cpp_lib_string_view >= 202403std::cout<< s1 + water + s2 << end;// 重载 (4),然后重载 (1)#elsestd::cout<< s1 +std::string(water)+ s2 << end;// OK,但较低效#endif}
输出:
Hello world! Hello Waterworld!
[编辑]缺陷报告
下列更改行为的缺陷报告追溯地应用于以前出版的 C++ 标准。
缺陷报告 | 应用于 | 出版时的行为 | 正确行为 |
---|---|---|---|
P1165R1 | C++11 | 分配器传播混乱且不一致 | 使之更为一致 |
[编辑]参阅
后附字符到结尾 (公开成员函数) | |
后附字符到结尾 (公开成员函数) | |
插入字符 (公开成员函数) |