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 > constexpr std::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 > constexpr std::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 > constexpr std::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 > constexpr std::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()
换句话说
在每种情况下,当两者都是相同值类别的 `basic_string` 时,首选左操作数。 对于 (8-16),所有右值 `basic_string` 操作数都处于有效但未指定的状态。 |
(C++11 起) |
目录 |
[编辑] 参数
lhs | - | 字符串,字符串视图(C++26 起),字符,或指向空终止数组中第一个字符的指针 |
rhs | - | 字符串,字符串视图(C++26 起),字符,或指向空终止数组中第一个字符的指针 |
[编辑] 返回值
一个字符串,包含来自 lhs 的字符后跟来自 rhs 的字符,使用上述确定的分配器(C++11 起)。
注意当涉及有状态分配器时(例如当使用 std::pmr::string 时),应非常谨慎地使用 由于 `operator+` 结果使用的分配器对值类别敏感,因此 `operator+` 在分配器传播方面不具有结合性。 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() + /* ... */; // uses select_on_container_copy_construction on meow's allocator woof + dog() + /* ... */; // uses allocator of dog()'s return value instead meow + woof + meow; // uses select_on_container_copy_construction on meow's allocator meow + (woof + meow); // uses SOCCC on woof's allocator instead 对于一连串 `operator+` 调用,可以通过在所需分配器前面加上一个右值 `basic_string` 来控制最终结果所使用的分配器。 // use my_favorite_allocator for the final result my_string(my_favorite_allocator) + meow + woof + cat() + dog(); 为了更好地、更可移植地控制分配器,应在构造了所需分配器的结果字符串上使用诸如 |
(C++11 起) |
在重载 (4)、(7)、(12) 和 (16) 中将 std::type_identity_t 用作参数,确保类型为 std::basic_string<CharT, Traits, Allocator> 的对象始终可以与隐式转换为 std::basic_string_view<CharT, Traits> 的 `T` 类型的对象进行连接,反之亦然,根据重载决议规则。
|
(C++26 起) |
[编辑] 示例
#include <iostream> #include <string> #include <string_view> int main() { std::string s1 = "Hello"; std::string s2 = "world"; const char* end = "!\n"; std::cout << s1 + ' ' + s2 + end; std::string_view water{" Water"}; #if __cpp_lib_string_view >= 202403 std::cout << s1 + water + s2 << end; // overload (4), then (1) #else std::cout << s1 + std::string(water) + s2 << end; // OK, but less efficient #endif }
输出
Hello world! Hello Waterworld!
[编辑] 缺陷报告
下列更改行为的缺陷报告追溯地应用于以前出版的 C++ 标准。
缺陷报告 | 应用于 | 发布时的行为 | 正确的行为 |
---|---|---|---|
P1165R1 | C++11 | 分配器传播随意且不一致 | 使其更一致 |
[编辑] 另请参阅
将字符追加到末尾 (public member function) | |
将字符追加到末尾 (public member function) | |
插入字符 (public member function) |