public member function
<utility>

std::pair::operator=

note
// implicitly declared:
copy (1)
pair& operator= (const pair& pr);
copy (1)
pair& operator= (const pair& pr);template <class U, class V>  pair& operator= (const pair<U,V>& pr);
move (2)
pair& operator= (pair&& pr)           noexcept (is_nothrow_move_assignable<first_type>::value &&                     is_nothrow_move_assignable<second_type>::value);template <class U, class V>  pair& operator= (pair<U,V>&& pr);
Assign contents
Assigns pr as the new content for the pair object.

Member first is assigned pr.first, and member second is assigned pr.second.

The copying forms (1) perform copy assignments, with the elements of its argument preserving their values after the call. While the moving forms (2) perform move assignments (as if calling forward for each element), which, for elements of types supporting move semantics implies that these elements of pr are left in an unspecified but valid state.

Parameters

pr
Another pair object.
This may be an object of the same type or of a pair type whose elements' types are implicitly convertible.

Return value

*this

Example

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
// pair::operator= example
#include <utility>      // std::pair, std::make_pair
#include <string>       // std::string
#include <iostream>     // std::cout

int main () {
  std::pair <std::string,int> planet, homeplanet;

  planet = std::make_pair("Earth",6371);

  homeplanet = planet;

  std::cout << "Home planet: " << homeplanet.first << '\n';
  std::cout << "Planet size: " << homeplanet.second << '\n';
  return 0;
}
Output:
Home planet: Earth
Planet size: 6371


Data races

All copied elements are accessed.
The object and both its first and second members are modified.
The moving assignments (2) also modify pr if any of its component types support move semantics for this assignment.

Exception safety

If none of the individual assignment operations performed on the members of pair can throw, the function never throws exceptions (no-throw guarantee).
Otherwise, if any of the forms taking an rvalue reference as argument is called, and at least one of the types in the pair can be assigned with move semantics, the operation may leave either or both pair objects in an invalid state in case of exception (no guarantee).
Otherwise, the operation guarantees that both pair objects involved end up in a valid state in case of exception (basic guarantee).

See also