命名空间
变体
操作

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) (C++20 起为 constexpr)
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) (C++20 起为 constexpr)
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) (C++20 起为 constexpr)
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) (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) (C++20 起为 constexpr)
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) (C++20 起为 constexpr)
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) (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) (C++11 起)
(C++20 起为 constexpr)
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) (C++11 起)
(C++20 起为 constexpr)
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) (C++11 起)
(C++20 起为 constexpr)
template< class CharT, class Traits, class Alloc >

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

               CharT rhs );
(11) (C++11 起)
(C++20 起为 constexpr)
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) (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) (C++11 起)
(C++20 起为 constexpr)
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) (C++11 起)
(C++20 起为 constexpr)
template< class CharT, class Traits, class Alloc >

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

               std::basic_string<CharT,Traits,Alloc>&& rhs );
(15) (C++11 起)
(C++20 起为 constexpr)
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) (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` 右值,则使用其分配器。
  • 否则,`select_on_container_copy_construction` 将用于左值 `basic_string` 操作数的分配器。

在每种情况下,当两者都是相同值类别的 `basic_string` 时,首选左操作数。

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

(C++11 起)

目录

[编辑] 参数

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

[编辑] 返回值

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

注意

当涉及有状态分配器时(例如当使用 std::pmr::string 时),应非常谨慎地使用 operator+ (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+= 等成员函数。

(C++11 起)

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

特性测试 标准 特性
__cpp_lib_string_view 202403 (C++26) 字符串和字符串视图的连接,重载 (4)(7)(12)(16)
(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) [编辑]