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) |