std::flat_map<Key,T,Compare,KeyContainer,MappedContainer>::operator=
来自cppreference.com
<tbody>
</tbody>
flat_map& operator=( const flat_map& other ); |
(1) | (C++23 起) (隐式声明) |
flat_map& operator=( flat_map&& other ); |
(2) | (C++23 起) (隐式声明) |
flat_map& operator=( std::initializer_list<value_type> ilist ); |
(3) | (C++23 起) |
以给定实参的内容替换容器适配器的内容。
1) 复制赋值运算符。以
other 的内容副本替换内容。相当于调用 c = other.c; comp = other.comp;。2) 移动赋值运算符。用移动语义以
other 的内容替换内容。相当于调用 c = std::move(other.c); comp = std::move(other.comp);。3) 以初始化式列表
ilist 所标识的内容替换内容。参数
| other | - | 用作源的另一容器适配器 |
| ilist | - | 用作源的初始化式列表 |
返回值
*this
复杂度
1,2) 等价于底层容器的
operator= 的复杂度。3) 与
*this 和 ilist 的大小成线性。示例
运行此代码
#include <flat_map>
#include <initializer_list>
#include <print>
#include <utility>
int main()
{
std::flat_map<int, int> x{{1, 1}, {2, 2}, {3, 3}}, y, z;
const auto w = {std::pair<const int, int>{4, 4}, {5, 5}, {6, 6}, {7, 7}};
std::println("起初:");
std::println("x = {}", x);
std::println("y = {}", y);
std::println("z = {}", z);
y = x; // 重载 (1)
std::println("复制赋值从 x 向 y 复制数据:");
std::println("x = {}", x);
std::println("y = {}", y);
z = std::move(x); // 重载 (2)
std::println("移动赋值从 x 向 z 移动数据,同时修改 x 和 z:");
std::println("x = {}", x);
std::println("z = {}", z);
z = w; // 重载 (3)
std::println("赋值 initializer_list w 给 z:");
std::println("w = {}", w);
std::println("z = {}", z);
}
输出:
起初:
x = {1: 1, 2: 2, 3: 3}
y = {}
z = {}
复制赋值从 x 向 y 复制数据:
x = {1: 1, 2: 2, 3: 3}
y = {1: 1, 2: 2, 3: 3}
移动赋值从 x 向 z 移动数据,同时修改 x 和 z:
x = {}
z = {1: 1, 2: 2, 3: 3}
赋值 initializer_list w 给 z:
w = {4: 4, 5: 5, 6: 6, 7: 7}
z = {4: 4, 5: 5, 6: 6, 7: 7}
参阅
构造 flat_map (公开成员函数) | |
| 替换底层各容器 (公开成员函数) |