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 | - | 字符串、字符串視圖(C++26 起)、字符或指向空終止字符序列首字符的指針 |
rhs | - | 字符串、字符串視圖(C++26 起)、字符或指向空終止字符序列首字符的指針 |
[編輯] 返回值
含有來自 lhs 的字符後隨來自 rhs 的字符的字符串,使用如上確定的分配器(C++11 起)。
註解涉及有狀態分配器時(例如用 std::pmr::string 時)(C++17 起),應該謹慎使用
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 的分配器上的 SOCCC meow + (woof + meow); // 转而使用 woof 的分配器上的 select_on_container_copy_construction 對於 // 令最终结果使用 my_favorite_allocator my_string(my_favorite_allocator) + meow + woof + cat() + dog(); 為了更好且可移植地對分配器進行控制,應該在以所欲分配器構造的結果字符串上,使用 | (C++11 起) |
根據重載決議規則,使用 std::type_identity_t 作為重載 (4)、(7)、(12) 和 (16) 的形參,保證了 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; // 重载 (4),然后重载 (1) #else std::cout << s1 + std::string(water) + s2 << end; // OK,但较低效 #endif }
輸出:
Hello world! Hello Waterworld!
[編輯] 缺陷報告
下列更改行為的缺陷報告追溯地應用於以前出版的 C++ 標準。
缺陷報告 | 應用於 | 出版時的行為 | 正確行為 |
---|---|---|---|
P1165R1 | C++11 | 分配器傳播混亂且不一致 | 使之更為一致 |
[編輯] 參閱
後附字符到結尾 (公開成員函數) | |
後附字符到結尾 (公開成員函數) | |
插入字符 (公開成員函數) |