c++11 标准模板(STL)(std::pair)(三)

定义于头文件 <utility>

std::pair 是一个结构体模板,其可于一个单元存储两个相异对象。 pair 是 std::tuple 的拥有两个元素的特殊情况。

对内容赋值

std::pair<T1,T2>::operator=

pair& operator=( const pair& other );

(1) (C++20 前)

constexpr pair& operator=( const pair& other );

(C++20 起)

template< class U1, class U2 >
pair& operator=( const pair<U1,U2>& other );

(2) (C++20 前)

template< class U1, class U2 >
constexpr pair& operator=( const pair<U1,U2>& other );

(C++20 起)

pair& operator=( pair&& other ) noexcept(/* see below */);

(3) (C++11 起)
(C++20 前)

constexpr pair& operator=( pair&& other ) noexcept(/* see below */);

(C++20 起)

template< class U1, class U2 >
pair& operator=( pair<U1,U2>&& other );

(4) (C++11 起)
(C++20 前)

template< class U1, class U2 >
constexpr pair& operator=( pair<U1,U2>&& other );

(C++20 起)

替换 pair 的内容。

1) 复制赋值运算符。以 other 内容的副本替换内容。

2) 赋值 other.firstfirstother.secondsecond

3) 移动赋值运算符。用移动语义以 other 的内容替换内容。

4) 赋值 std::forward<U1>(p.first) 给 first , std::forward<U2>(p.second) 给 second

这些函数的行为未定义,除非:

  • 对于 (1) , std::is_copy_assignable<first_type>::value 与 std::is_copy_assignable<second_type>::value 皆为 true 。
  • 对于 (2) , std::is_assignable<first_type&, const U1&>::value 与 std::is_assignable<second_type&, const U2&>::value 皆为 true 。
  • 对于 (3) , std::is_move_assignable<first_type>::value 与 std::is_move_assignable<second_type>::value 皆为 true 。
  • 对于 (4) , std::is_assignable<first_type&, U1&&>::value 与 std::is_assignable<second_type&, U2&&>::value 皆为 true 。
(C++17 前)

这些函数不参与重载决议(或对于复制赋值运算符,定义为被删除),若任何要求的操作非法。按顺序为:

  • (1) 定义为被删除,除非 std::is_copy_assignable_v<first_type> 与 std::is_copy_assignable_v<second_type> 皆为 true 。
  • (2) 不参与重载决议,除非 std::is_assignable_v<first_type&, const U1&> 与 std::is_assignable_v<second_type&, const U2&> 皆为 true 。
  • (3) 不参与重载决议,除非 std::is_move_assignable_v<first_type> 与 std::is_move_assignable_v<second_type> 皆为 true 。
  • (4) 不参与重载决议,除非 std::is_assignable_v<first_type&, U1&&> 与 std::is_assignable_v<second_type&, U2&&> 皆为 true 。

参数

other - 替换此 pair 的值的 pair

返回值

*this

异常

1-2) (无)

3)noexcept 规定:  noexcept(

    is_nothrow_move_assignable<T1>::value &&
    is_nothrow_move_assignable<T2>::value)

4) (无)

调用示例

#include <iostream>
#include <string>
#include <iomanip>
#include <complex>
#include <tuple>

struct Cell
{
    int x;
    int y;

    Cell() = default;
    Cell(int a, int b): x(a), y(b) {}
};

std::ostream &operator<<(std::ostream &os, const Cell &cell)
{
    os << "{" << cell.x << "," << cell.y << "}";
    return os;
}

int main()
{
    //以 x 初始化 first 并以 y 初始化 second 。
    std::pair<int, Cell> spair(101, Cell(102, 103));
    std::cout << "spair:" << std::setw(8) << spair.first << "    " << spair.second << std::endl;

    //1) 复制赋值运算符。以 other 内容的副本替换内容。
    std::pair<int, Cell> pair1 = spair;
    std::cout << "pair1:" << std::setw(8) << pair1.first << "    " << pair1.second << std::endl;

    //2) 赋值 other.first 给 first , other.second 给 second
    std::pair<int, Cell> pair2 = {201, Cell(202, 203)};
    std::cout << "pair2:" << std::setw(8) << pair2.first << "    " << pair2.second << std::endl;

    //3) 移动赋值运算符。用移动语义以 other 的内容替换内容。
    std::pair<int, Cell> pair3 = std::move(spair);
    std::cout << "pair3:" << std::setw(8) << pair3.first << "    " << pair3.second << std::endl;

    //4) 赋值 std::forward<U1>(p.first) 给 first , std::forward<U2>(p.second) 给 second 。
    std::pair<int, Cell> pair4 = {std::move(201), std::move(Cell(202, 203))};
    std::cout << "pair4:" << std::setw(8) << pair4.first << "    " << pair4.second << std::endl;

    return 0;
}

输出

spair:     101    {102,103}
pair1:     101    {102,103}
pair2:     201    {202,203}
pair3:     101    {102,103}
pair4:     201    {202,203}

猜你喜欢

转载自blog.csdn.net/qq_40788199/article/details/135006305