std::basic_string<CharT,Traits,Allocator>::operator=
來自 cppreference.com
< cpp | string | basic_string
basic_string& operator=( const basic_string& str ); |
(1) | (C++20 起為 constexpr) |
basic_string& operator=( basic_string&& str ) noexcept(/* 見下 */); |
(2) | (C++11 起) (C++20 起為 constexpr) |
basic_string& operator=( const CharT* s ); |
(3) | (C++20 起為 constexpr) |
basic_string& operator=( CharT ch ); |
(4) | (C++20 起為 constexpr) |
basic_string& operator=( std::initializer_list<CharT> ilist ); |
(5) | (C++11 起) (C++20 起為 constexpr) |
template<class StringViewLike> basic_string& operator=( const StringViewLike& t ); |
(6) | (C++17 起) (C++20 起為 constexpr) |
basic_string& operator=( std::nullptr_t ) = delete; |
(7) | (C++23 起) |
替換字串的內容。
1) 將內容替換為 str 的副本。如果 *this 和 str 是同一個物件,則此函式無效。
與其他序列容器的移動賦值不同,指向 str 元素的引用、指標和迭代器可能會失效。
3) 替換內容為 s 指向的以空字元結尾的字串,如同透過 assign(s, Traits::length(s))。
5) 替換內容為初始化列表 ilist 的內容,如同透過 assign(ilist.begin(), ilist.size())。
6) 隱式將 t 轉換為字串檢視 sv,如同透過 std::basic_string_view<CharT, Traits> sv = t;,然後替換內容為 sv 的內容,如同透過 assign(sv)。
此過載僅當 std::is_convertible_v<const StringViewLike&,
std::basic_string_view<CharT, Traits>> 為 true 且 std::is_convertible_v<const StringViewLike&, const CharT*> 為 false 時才參與過載決議。
std::basic_string_view<CharT, Traits>> 為 true 且 std::is_convertible_v<const StringViewLike&, const CharT*> 為 false 時才參與過載決議。
7)
std::basic_string
不能從 nullptr 賦值。目錄 |
[編輯] 引數
ch | - | 用於初始化字串字元的值 |
str | - | 用作源以初始化字串的字串 |
s | - | 指向以空字元結尾的字串的指標,用作源以初始化字串 |
ilist | - | std::initializer_list 用於初始化字串 |
t | - | 可轉換為 std::basic_string_view 的物件,用於初始化字串 |
[編輯] 返回值
*this
[編輯] 複雜度
1) 與 str 的大小成線性關係。
2) 與 *this 的大小成線性關係(形式上,每個
CharT
都必須被銷燬)。如果分配器不相等且不傳播,則也與 str 的大小成線性關係(必須進行復制)。3) 與 s 的大小成線性關係。
4) 常數時間。
5) 與 ilist 的大小成線性關係。
6) 與 t 的大小成線性關係。
[編輯] 異常
2)
noexcept 規範:
noexcept(std::allocator_traits<Allocator>::
propagate_on_container_move_assignment::value ||
如果操作會導致 size()
超過 max_size()
,則丟擲 std::length_error。
如果由於任何原因丟擲異常,此函式無效果(強異常安全保證)。
[編輯] 示例
執行此程式碼
#include <iomanip> #include <iostream> #include <string> int main() { std::string str1; std::string str2{"alpha"}; // (1) operator=(const basic_string&); str1 = str2; std::cout << std::quoted(str1) << ' ' // "alpha" << std::quoted(str2) << '\n'; // "alpha" // (2) operator=(basic_string&&); str1 = std::move(str2); std::cout << std::quoted(str1) << ' ' // "alpha" << std::quoted(str2) << '\n'; // "" or "alpha" (unspecified) // (3) operator=(const CharT*); str1 = "beta"; std::cout << std::quoted(str1) << '\n'; // "beta" // (4) operator=(CharT); str1 = '!'; std::cout << std::quoted(str1) << '\n'; // "!" // (5) operator=(std::initializer_list<CharT>); str1 = {'g', 'a', 'm', 'm', 'a'}; std::cout << std::quoted(str1) << '\n'; // "gamma" // (6) operator=(const T&); str1 = 35U; // equivalent to str1 = static_cast<char>(35U); std::cout << std::quoted(str1) << '\n'; // "#" (ASCII = 35) }
可能的輸出
"alpha" "alpha" "alpha" "" "beta" "!" "gamma" "#"
[編輯] 缺陷報告
下列更改行為的缺陷報告追溯地應用於以前出版的 C++ 標準。
缺陷報告 | 應用於 | 釋出時的行為 | 正確的行為 |
---|---|---|---|
LWG 847 | C++98 | 沒有異常安全保證 | 添加了強異常安全保證 |
LWG 2063 | C++11 | 移動賦值運算子未遵循 SequenceContainer 的語義要求 |
已遵循 |
LWG 2946 | C++17 | 過載 (6) 在某些情況下導致歧義 | 透過使其成為模板來避免 |
[編輯] 參閱
構造一個 basic_string (公共成員函式) | |
給字串賦值字元 (公共成員函式) | |
賦值檢視 ( std::basic_string_view<CharT,Traits> 的公共成員函式) |