命名空间
变体
操作

std::operator+(std::basic_string)

来自 cppreference.cn
< cpp‎ | string‎ | basic string
 
 
 
std::basic_string
 
定义于头文件 <string>
template< class CharT, class Traits, class Alloc >

std::basic_string<CharT,Traits,Alloc>
    operator+( const std::basic_string<CharT,Traits,Alloc>& lhs,

               const std::basic_string<CharT,Traits,Alloc>& rhs );
(1) (constexpr since C++20)
template< class CharT, class Traits, class Alloc >

std::basic_string<CharT,Traits,Alloc>
    operator+( const std::basic_string<CharT,Traits,Alloc>& lhs,

               const CharT* rhs );
(2) (constexpr since C++20)
template< class CharT, class Traits, class Alloc >

std::basic_string<CharT,Traits,Alloc>
    operator+( const std::basic_string<CharT,Traits,Alloc>& lhs,

               CharT rhs );
(3) (constexpr since C++20)
template< class CharT, class Traits, class Alloc >

constexpr std::basic_string<CharT,Traits,Alloc>
    operator+( const std::basic_string<CharT,Traits,Alloc>& lhs,

               std::type_identity_t<std::basic_string_view<CharT,Traits>> rhs );
(4) (since C++26)
template< class CharT, class Traits, class Alloc >

std::basic_string<CharT,Traits,Alloc>
    operator+( const CharT* lhs,

               const std::basic_string<CharT,Traits,Alloc>& rhs );
(5) (constexpr since C++20)
template< class CharT, class Traits, class Alloc >

std::basic_string<CharT,Traits,Alloc>
    operator+( CharT lhs,

               const std::basic_string<CharT,Traits,Alloc>& rhs );
(6) (constexpr since C++20)
template< class CharT, class Traits, class Alloc >

constexpr std::basic_string<CharT,Traits,Alloc>
    operator+( std::type_identity_t<std::basic_string_view<CharT,Traits>> lhs,

               const std::basic_string<CharT,Traits,Alloc>& rhs );
(7) (since C++26)
template< class CharT, class Traits, class Alloc >

std::basic_string<CharT,Traits,Alloc>
    operator+( std::basic_string<CharT,Traits,Alloc>&& lhs,

               std::basic_string<CharT,Traits,Alloc>&& rhs );
(8) (since C++11)
(constexpr since C++20)
template< class CharT, class Traits, class Alloc >

std::basic_string<CharT,Traits,Alloc>
    operator+( std::basic_string<CharT,Traits,Alloc>&& lhs,

               const std::basic_string<CharT,Traits,Alloc>& rhs );
(9) (since C++11)
(constexpr since C++20)
template< class CharT, class Traits, class Alloc >

std::basic_string<CharT,Traits,Alloc>
    operator+( std::basic_string<CharT,Traits,Alloc>&& lhs,

               const CharT* rhs );
(10) (since C++11)
(constexpr since C++20)
template< class CharT, class Traits, class Alloc >

std::basic_string<CharT,Traits,Alloc>
    operator+( std::basic_string<CharT,Traits,Alloc>&& lhs,

               CharT rhs );
(11) (since C++11)
(constexpr since C++20)
template< class CharT, class Traits, class Alloc >

constexpr std::basic_string<CharT,Traits,Alloc>
    operator+( std::basic_string<CharT,Traits,Alloc>&& lhs,

               std::type_identity_t<std::basic_string_view<CharT,Traits>> rhs );
(12) (since C++26)
template< class CharT, class Traits, class Alloc >

std::basic_string<CharT,Traits,Alloc>
    operator+( const std::basic_string<CharT,Traits,Alloc>& lhs,

               std::basic_string<CharT,Traits,Alloc>&& rhs );
(13) (since C++11)
(constexpr since C++20)
template< class CharT, class Traits, class Alloc >

std::basic_string<CharT,Traits,Alloc>
    operator+( const CharT* lhs,

               std::basic_string<CharT,Traits,Alloc>&& rhs );
(14) (since C++11)
(constexpr since C++20)
template< class CharT, class Traits, class Alloc >

std::basic_string<CharT,Traits,Alloc>
    operator+( CharT lhs,

               std::basic_string<CharT,Traits,Alloc>&& rhs );
(15) (since C++11)
(constexpr since C++20)
template< class CharT, class Traits, class Alloc >

constexpr std::basic_string<CharT,Traits,Alloc>
    operator+( std::type_identity_t<std::basic_string_view<CharT,Traits>> lhs,

               std::basic_string<CharT,Traits,Alloc>&& rhs );
(16) (since C++26)

返回一个字符串,包含来自 lhs 的字符,后跟来自 rhs 的字符。等效于

1,2) std::basic_string<CharT, Traits, Allocator> r = lhs; r.append(rhs); return r;
3) std::basic_string<CharT, Traits, Allocator> r = lhs; r.push_back(rhs); return r;
4) std::basic_string<CharT, Traits, Allocator> r = lhs; r.append(rhs); return r;
5) std::basic_string<CharT, Traits, Allocator> r = rhs; r.insert(0, lhs); return r;
6) std::basic_string<CharT, Traits, Allocator> r = rhs; r.insert(r.begin(), lhs); return r;
7) std::basic_string<CharT, Traits, Allocator> r = rhs; r.insert(0, lhs); return r;
8) lhs.append(rhs); return std::move(lhs); 除了 lhsrhs 都处于有效但不指定的状态。如果 lhsrhs 具有相同的分配器,则实现可以从任一对象移动。
9,10) lhs.append(rhs); return std::move(lhs);
11) lhs.push_back(rhs); return std::move(lhs);
12) lhs.append(rhs); return std::move(lhs);
13,14) rhs.insert(0, lhs); return std::move(rhs);
15) rhs.insert(rhs.begin(), lhs); return std::move(rhs);
16) rhs.insert(0, lhs); return std::move(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 右值,则使用其分配器。
  • 否则,对左值 basic_string 操作数的分配器使用 select_on_container_copy_construction

在每种情况下,当两个操作数都是相同值类别的 basic_string 时,优先选择左操作数。

对于 (8-16),所有右值 basic_string 操作数都处于有效但不指定的状态。

(since C++11)

目录

[编辑] 参数

lhs - 字符串,字符串视图(C++26 起)、字符或指向空终止数组中首字符的指针
rhs - 字符串,字符串视图(C++26 起)、字符或指向空终止数组中首字符的指针

[编辑] 返回值

一个字符串,包含来自 lhs 的字符,后跟来自 rhs 的字符,使用如上所述确定的分配器(C++11 起)

注解

当涉及有状态分配器时,应非常谨慎地使用 operator+(例如当使用 std::pmr::string 时)(C++17 起)。在 P1165R1 之前,用于结果的分配器是由历史偶然性决定的,并且可能因重载而异,没有明显的原因。此外,对于 (1-5),分配器传播行为在主要的标准库实现中有所不同,并且与标准中描述的行为不同。

由于 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();

为了更好地、可移植地控制分配器,应在用所需分配器构造的结果字符串上使用成员函数,如 appendinsertoperator+=

(since C++11)

重载 (4)(7)(12)(16) 中使用 std::type_identity_t 作为参数,确保了类型为 std::basic_string<CharT, Traits, Allocator> 的对象始终可以连接到类型为 T 的对象,后者可以隐式转换为 std::basic_string_view<CharT, Traits>,反之亦然,根据 重载决议 规则。

特性测试 Std 特性
__cpp_lib_string_view 202403 (C++26) 字符串和字符串视图的连接,重载 (4)(7)(12)(16)
(since 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 分配器传播是随意且不一致的 变得更加一致

[编辑] 参见

将字符追加到末尾
(公共成员函数) [编辑]
将字符追加到末尾
(公共成员函数) [编辑]
插入字符
(公共成员函数) [编辑]