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
(
)
换言之:
在两种操作数均为相同值类别的
对于
(
8-16
)
,所有右值
|
(C++11 起) |
目录 |
参数
| lhs | - | string , string view (since C++26) , character, or pointer to the first character in a null-terminated array |
| rhs | - | string , string view (since C++26) , character, or pointer to the first character in a null-terminated array |
返回值
一个包含来自 lhs 的字符,后接来自 rhs 的字符所组成的字符串 ,使用如上确定的分配器 (C++11 起) 。
注解
由于
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 的分配器使用 select_on_container_copy_construction meow + (woof + meow); // 改为对 woof 的分配器使用 SOCCC
对于连续的
// 为最终结果使用 my_favorite_allocator 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
>
能力的
|
(自 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++ 标准。
| DR | 适用版本 | 发布行为 | 正确行为 |
|---|---|---|---|
| P1165R1 | C++11 | 分配器传播存在随意性和不一致性 | 使其更加一致 |
另请参阅
|
向末尾追加字符
(公开成员函数) |
|
|
向末尾追加字符
(公开成员函数) |
|
|
插入字符
(公开成员函数) |