std::inplace_vector<T,N>::operator=
來自 cppreference.com
< cpp | 容器 | inplace_vector
constexpr inplace_vector& operator=( const inplace_vector& other ); |
(1) | (C++26 起) |
constexpr inplace_vector& operator=( inplace_vector&& other ) noexcept(/* 見下 */); |
(2) | (C++26 起) |
constexpr inplace_vector& operator=( std::initializer_list<T> init ); |
(3) | (C++26 起) |
替換 inplace_vector
的內容。
1) 複製賦值運算子。如果 std::inplace_vector<T, N> 具有平凡解構函式,且 std::is_trivially_copy_constructible_v<T> && std::is_trivially_copy_assignable_v<T> 為 true,則它也是一個平凡複製賦值運算子。用 other 內容的副本替換當前內容。
2) 移動賦值運算子。如果 std::inplace_vector<T, N> 具有平凡解構函式,且 std::is_trivially_move_constructible_v<T> && std::is_trivially_move_assignable_v<T> 為 true,則它也是一個平凡移動賦值運算子。使用移動語義(即,other 中的資料從 other 移動到此容器中)替換內容。之後 other 處於有效但未指定的狀態。
3) 用初始化列表 init 標識的內容替換當前內容。
目錄 |
[編輯] 引數
其他 | - | 另一個 inplace_vector ,用作初始化容器元素的源 |
init | - | 用於初始化容器元素的初始化列表 |
[編輯] 複雜度
1,2) 與 *this 和 other 的大小呈線性關係。
3) 與 *this 和 init 的大小呈線性關係。
[編輯] 異常
2)
noexcept 規範:
noexcept(N == 0 ||
(std::is_nothrow_move_assignable_v<T> &&
[編輯] 示例
執行此程式碼
#include <initializer_list> #include <inplace_vector> #include <new> #include <print> #include <ranges> #include <string> int main() { std::inplace_vector<int, 4> x({1, 2, 3}), y; std::println("Initially:"); std::println("x = {}", x); std::println("y = {}", y); std::println("Copy assignment copies data from x to y:"); y = x; // overload (1) std::println("x = {}", x); std::println("y = {}", y); std::inplace_vector<std::string, 3> z, w{"\N{CAT}", "\N{GREEN HEART}"}; std::println("Initially:"); std::println("z = {}", z); std::println("w = {}", w); std::println("Move assignment moves data from w to z:"); z = std::move(w); // overload (2) std::println("z = {}", z); std::println("w = {}", w); // w is in valid but unspecified state auto l = {4, 5, 6, 7}; std::println("Assignment of initializer_list {} to x:", l); x = l; // overload (3) std::println("x = {}", x); std::println("Assignment of initializer_list with size bigger than N throws:"); try { x = {1, 2, 3, 4, 5}; // throws: (initializer list size == 5) > (capacity N == 4) } catch(const std::bad_alloc& ex) { std::println("ex.what(): {}", ex.what()); } }
可能的輸出
Initially: x = [1, 2, 3] y = [] Copy assignment copies data from x to y: x = [1, 2, 3] y = [1, 2, 3] Initially: z = [] w = ["🐈", "💚"] Move assignment moves data from w to z: z = ["🐈", "💚"] w = ["", ""] Assignment of initializer_list [4, 5, 6, 7] to x: x = [4, 5, 6, 7] Assignment of initializer_list with size bigger than N throws: ex.what(): std::bad_alloc
[編輯] 參閱
構造 inplace_vector (public member function) | |
將值賦給容器 (public member function) |