public member function
<unordered_map>

std::unordered_map::operator=

copy (1)
unordered_map& operator= ( const unordered_map& ump );
move (2)
unordered_map& operator= ( unordered_map&& ump );
initializer list (3)
unordered_map& operator= ( intitializer_list<value_type> il );
Assign content
Assigns ump (or il) as the new content for the container.

The elements contained in the object before the call are destroyed, and replaced by those in unordered_map ump or initializer list il, if any.

The first version (1) performs a copy assignment, which copies all the elements of ump into the container object (with ump preserving its contents).

The second version (2) performs a move assignment, which transfer ownership of the contents of ump to the object. No copies occur: the content is lost by ump.

The third version (3) assigns the contents of the initializer list il as the elements of the container object.

参数

ump
An unordered_map object of the same type (i.e., with the same template parameters).
il
一个 initializer_list 对象。编译器将自动从初始化列表声明器创建此类对象。
成员类型value_typeis the type of the elements contained in the unordered_map, which ispair<const key_type,mapped_type>,其中成员类型key_type是第一个模板参数(键类型)的别名,而mapped_type是第二个模板参数(映射类型,T).

返回值

*this

示例

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
// assignment operator with unordered_map
#include <iostream>
#include <string>
#include <unordered_map>

typedef std::unordered_map<std::string,std::string> stringmap;

stringmap merge (stringmap a,stringmap b) {
  stringmap temp(a); temp.insert(b.begin(),b.end()); return temp;
}

int main ()
{
  stringmap first, second, third;
  first = {{"AAPL","Apple"},{"MSFT","Microsoft"}};  // init list
  second = {{"GOOG","Google"},{"ORCL","Oracle"}};   // init list
  third = merge(first,second);                      // move
  first = third;                                    // copy

  std::cout << "first contains:";
  for (auto& elem: first) std::cout << " " << elem.first << ":" << elem.second;
  std::cout << std::endl;

  return 0;
}

可能的输出
first contains: MSFT:Microsoft AAPL:Apple GOOG:Google ORCL:Oracle


复杂度

For the copy assignment (1): Linear in sizes (destructions, copies).
For the move assignment (2): Linear in current container size (destructions).*
For the initializer list assignment (3): On average, linear in sizes (destructions, move-assignments) -- worst case: quadratic.
* Additional complexity for assignments if allocators do not propagate.

迭代器有效性

All iterators, references and pointers to elements that were in the container before the call are invalidated.

另见