名稱空間
變體
操作

std::operator+(std::basic_string)

來自 cppreference.com
< 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) [編輯]