跳到主要内容

std::multimap operator=

// (1) Non const version only
multimap& operator=( const multimap& other );

// (2) Non const version only
multimap& operator=( multimap&& other ) noexcept(/* see below */);

// (3) Non const version only
multimap& operator=( std::initializer_list<value_type> ilist );

替换容器的内容。

参数

  • other - 用作数据源的另一个容器
  • ilist - 用作数据源的初始化列表

返回值

对容器 *this 的引用。

异常

  • (1, 3) 可能抛出实现定义的异常。
  • (2) noexcept 规范
    noexcept(std::allocator_traits<Allocator>::is_always_equal::value
    && std::is_nothrow_move_assignable<Compare>::value)

复杂度

  • (1)*thisother 的大小上呈线性 - O(size() + other.size())
  • (2)
    *this 的大小呈线性关系 - O(size())。如果分配器不相等且不传播 - 与 *thisother 的大小呈线性关系 - O(size() + other)
  • (3)
    通常为 O(N log N),其中 Nsize() + ilist.size()。如果 ilist 根据 value_comp() 排序,则为线性关系 - O(size())

备注

在容器移动赋值(重载 (2))之后,除非不兼容的分配器强制进行逐元素移动赋值,否则指向 other 的引用、指针和迭代器(除了 end 迭代器)仍然有效,但它们引用的是现在位于 *this 中的元素。当前标准通过 [container.requirements.general]/12 中的全面声明作出此保证,并且正在通过 LWG 2321 考虑更直接的保证。

示例

Main.cpp
#include <map>
#include <iterator>
#include <iostream>
#include <utility>
#include <initializer_list>

void print(auto const comment, auto const& container)
{
auto size = std::size(container);
std::cout << comment << "{ ";
for (auto const& [key, value]: container)
std::cout << '{' << key << ',' << value << (--size ? "}, " : "} ");
std::cout << "}\n";
}

int main()
{
std::multimap<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::cout << "Initially:\n";
print("x = ", x);
print("y = ", y);
print("z = ", z);

std::cout << "Copy assignment copies data from x to y:\n";
y = x;
print("x = ", x);
print("y = ", y);

std::cout << "Move assignment moves data from x to z, modifying both x and z:\n";
z = std::move(x);
print("x = ", x);
print("z = ", z);

std::cout << "Assignment of initializer_list w to z:\n";
z = w;
print("w = ", w);
print("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} }
本文源自此 CppReference 页面。它可能为了改进或编辑者的偏好而进行了修改。点击“编辑此页面”查看此文档的所有更改。
悬停查看原始许可证。

std::multimap operator=

// (1) Non const version only
multimap& operator=( const multimap& other );

// (2) Non const version only
multimap& operator=( multimap&& other ) noexcept(/* see below */);

// (3) Non const version only
multimap& operator=( std::initializer_list<value_type> ilist );

替换容器的内容。

参数

  • other - 用作数据源的另一个容器
  • ilist - 用作数据源的初始化列表

返回值

对容器 *this 的引用。

异常

  • (1, 3) 可能抛出实现定义的异常。
  • (2) noexcept 规范
    noexcept(std::allocator_traits<Allocator>::is_always_equal::value
    && std::is_nothrow_move_assignable<Compare>::value)

复杂度

  • (1)*thisother 的大小上呈线性 - O(size() + other.size())
  • (2)
    *this 的大小呈线性关系 - O(size())。如果分配器不相等且不传播 - 与 *thisother 的大小呈线性关系 - O(size() + other)
  • (3)
    通常为 O(N log N),其中 Nsize() + ilist.size()。如果 ilist 根据 value_comp() 排序,则为线性关系 - O(size())

备注

在容器移动赋值(重载 (2))之后,除非不兼容的分配器强制进行逐元素移动赋值,否则指向 other 的引用、指针和迭代器(除了 end 迭代器)仍然有效,但它们引用的是现在位于 *this 中的元素。当前标准通过 [container.requirements.general]/12 中的全面声明作出此保证,并且正在通过 LWG 2321 考虑更直接的保证。

示例

Main.cpp
#include <map>
#include <iterator>
#include <iostream>
#include <utility>
#include <initializer_list>

void print(auto const comment, auto const& container)
{
auto size = std::size(container);
std::cout << comment << "{ ";
for (auto const& [key, value]: container)
std::cout << '{' << key << ',' << value << (--size ? "}, " : "} ");
std::cout << "}\n";
}

int main()
{
std::multimap<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::cout << "Initially:\n";
print("x = ", x);
print("y = ", y);
print("z = ", z);

std::cout << "Copy assignment copies data from x to y:\n";
y = x;
print("x = ", x);
print("y = ", y);

std::cout << "Move assignment moves data from x to z, modifying both x and z:\n";
z = std::move(x);
print("x = ", x);
print("z = ", z);

std::cout << "Assignment of initializer_list w to z:\n";
z = w;
print("w = ", w);
print("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} }
本文源自此 CppReference 页面。它可能为了改进或编辑者的偏好而进行了修改。点击“编辑此页面”查看此文档的所有更改。
悬停查看原始许可证。