命名空间
变体
操作

std::flat_map<Key,T,Compare,KeyContainer,MappedContainer>::operator=

来自 cppreference.com
 
 
 
 
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)*thisilist 的大小呈线性关系。

[编辑] 示例

#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("Initially:");
    std::println("x = {}", x);
    std::println("y = {}", y);
    std::println("z = {}", z);
 
    y = x; // overload (1)
    std::println("Copy assignment copies data from x to y:");
    std::println("x = {}", x);
    std::println("y = {}", y);
 
    z = std::move(x); // overload (2)
    std::println("Move assignment moves data from x to z, modifying both x and z:");
    std::println("x = {}", x);
    std::println("z = {}", z);
 
    z = w; // overload (3)
    std::println("Assignment of initializer_list w to z:");
    std::println("w = {}", w);
    std::println("z = {}", z);
}

输出

Initially:
x = {1: 1, 2: 2, 3: 3}
y = {}
z = {}
Copy assignment copies data from x to y:
x = {1: 1, 2: 2, 3: 3}
y = {1: 1, 2: 2, 3: 3}
Move assignment moves data from x to z, modifying both x and z:
x = {}
z = {1: 1, 2: 2, 3: 3}
Assignment of initializer_list w to z:
w = {4: 4, 5: 5, 6: 6, 7: 7}
z = {4: 4, 5: 5, 6: 6, 7: 7}

[编辑] 另请参阅

构造 flat_map
(公共成员函数) [编辑]
替换底层容器
(公共成员函数) [编辑]