std::flat_set<Key,Compare,KeyContainer>::operator=
来自cppreference.com
| |
(1) | (C++23 起) (隐式声明) |
| |
(2) | (C++23 起) (隐式声明) |
| |
(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_set>
#include <initializer_list>
#include <print>
int main()
{
std::flat_set<int> x{1, 2, 3}, y, z;
const auto w = {4, 5, 6, 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, 2, 3}
y = {}
z = {}
复制赋值从 x 向 y 复制数据:
x = {1, 2, 3}
y = {1, 2, 3}
移动赋值从 x 向 z 移动数据,同时修改 x 和 z:
x = {}
z = {1, 2, 3}
赋值 initializer_list w 给 z:
w = {4, 5, 6, 7}
z = {4, 5, 6, 7}
参阅
构造 flat_set (公开成员函数) | |
| 替换底层容器 (公开成员函数) |