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

定义于头文件 <utility>

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

构造函数

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

pair();

(1)(C++11 前)

constexpr pair();

(C++11 起)
(条件性 explicit)

pair( const T1& x, const T2& y );

(2)(C++11 前)

pair( const T1& x, const T2& y );

(C++11 起)
(C++14 前)
(条件性 explicit)

constexpr pair( const T1& x, const T2& y );

(C++14 起)
(条件性 explicit)

template< class U1, class U2 >
pair( U1&& x, U2&& y );

(3)(C++11 起)
(C++14 前)
(条件性 explicit)

template< class U1, class U2 >
constexpr pair( U1&& x, U2&& y );

(C++14 起)
(条件性 explicit)

template< class U1, class U2 >
pair( const pair<U1, U2>& p );

(4)(C++11 前)

template< class U1, class U2 >
pair( const pair<U1, U2>& p );

(C++11 起)
(C++14 前)
(条件性 explicit)

template< class U1, class U2 >
constexpr pair( const pair<U1, U2>& p );

(C++14 起)
(条件性 explicit)

template< class U1, class U2 >
pair( pair<U1, U2>&& p );

(5)(C++11 起)
(C++14 前)
(条件性 explicit)

template< class U1, class U2 >
constexpr pair( pair<U1, U2>&& p );

(C++14 起)
(条件性 explicit)
template< class... Args1, class... Args2 >

pair( std::piecewise_construct_t,
      std::tuple<Args1...> first_args,

      std::tuple<Args2...> second_args );
(6)(C++11 起)
(C++20 前)
template< class... Args1, class... Args2 >

constexpr pair( std::piecewise_construct_t,
                std::tuple<Args1...> first_args,

                std::tuple<Args2...> second_args );
(C++20 起)

pair( const pair& p ) = default;

(7)

pair( pair&& p ) = default;

(8)(C++11 起)

 

构造新的 pair

1) 默认构造函数。值初始化 pair 的两个元素 firstsecond

  • 此构造函数参与重载决议,当且仅当 std::is_default_constructible_v<first_type> 与 std::is_default_constructible_v<second_type> 均为 true 。
  • 此构造函数为 explicit ,当且仅当 first_typesecond_type 不可隐式默认构造。
(C++11 起)

2) 以 x 初始化 first 并以 y 初始化 second

  • 此构造函数参与重载决议,当且仅当 std::is_copy_constructible_v<first_type> 与 std::is_copy_constructible_v<second_type> 均为 true 。
  • 此构造函数为 explicit ,当且仅当 std::is_convertible_v<const first_type&, first_type> 为 false 或 std::is_convertible_v<const second_type&, second_type> 为 false 。
(C++11 起)

3) 以 std::forward<U1>(x) 初始化 first 并以 std::forward<U2>(y) 初始化 second

  • 此构造函数参与重载决议,当且仅当 std::is_constructible_v<first_type, U1&&> 和 std::is_constructible_v<second_type, U2&&> 均为 true 。
  • 此构造函数为 explicit ,当且仅当 std::is_convertible_v<U1&&, first_type> 为 false 或 std::is_convertible_v<U2&&, second_type> 为 false 。

4) 以 p.first 初始化 first 并以 p.second 初始化 second

  • 此构造函数参与重载决议,当且仅当 std::is_constructible_v<first_type, const U1&> 和 std::is_constructible_v<second_type, const U2&> 均为 true 。
  • 此构造函数为 explicit ,当且仅当 std::is_convertible_v<const U1&, first_type> 为 false 或 std::is_convertible_v<const U2&, second_type> 为 false 。
(C++11 起)

5) 以 std::forward<U1>(p.first) 初始化 first 并以 std::forward<U2>(p.second) 初始化 second

  • 此构造函数参与重载决议,当且仅当 std::is_constructible_v<first_type, U1&&> 和 std::is_constructible_v<second_type, U2&&> 均为 true 。
  • 此构造函数为 explicit ,当且仅当 std::is_convertible_v<U1&&, first_type> 为 false 或 std::is_convertible_v<U2&&, second_type> 为 false 。

6) 转发 first_args 的元素到 first 的构造函数并转发 second_args 的元素到 second 的构造函数。这是能用于构造不可复制不可移动类型的 pair 的仅有的非默认构造函数。

7) 复制构造函数为默认,且若两个元素的复制满足 constexpr 函数的要求则为 constexpr 。

8) 移动构造函数为默认,且若两个元素的移动满足 constexpr 函数的要求则为 constexpr 。

参数

x-初始化此 pair 首元素的值
y-初始化此 pair 第二元素的值
p-用于初始化此 pair 两个元素的值的 pair
first_args-初始化此 pair 首元素的构造函数参数的 tuple
second_args-初始化此 pair 第二元素的构造函数参数的 tuple

异常

不抛异常,除非指定操作之一(如元素的构造)抛出。

缺陷报告

下列更改行为的缺陷报告追溯地应用于以前出版的 C++ 标准。

DR应用于出版时的行为正确行为
N4387C++11某些构造函数曾为 explicit ,阻止了有用的行为使大多数构造函数为条件性 explicit
LWG 2510C++11默认构造函数为隐式使之为条件性 explicit

调用示例

#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()
{
    //1) 默认构造函数。值初始化 pair 的两个元素 first 和 second 。
    std::pair<int, Cell> pair1;
    std::cout << "pair1:" << std::setw(8) << pair1.first << "    " << pair1.second << std::endl;

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

    //3) 以 std::forward<U1>(x) 初始化 first 并以 std::forward<U2>(y) 初始化 second 。
    std::pair<int, Cell> pair3(std::move(201), std::move(Cell(202, 203)));
    std::cout << "pair3:" << std::setw(8) << pair3.first << "    " << pair3.second << std::endl;

    //4) 以 p.first 初始化 first 并以 p.second 初始化 second 。
    std::pair<int, Cell> pair4{501, Cell(502, 503)};
    std::cout << "pair4:" << std::setw(8) << pair4.first << "    " << pair4.second << std::endl;

    //5) 以 std::forward<U1>(p.first) 初始化 first 并以 std::forward<U2>(p.second) 初始化 second 。
    std::pair<int, Cell> pair5{std::move(601), std::move(Cell(602, 603))};
    std::cout << "pair5:" << std::setw(8) << pair5.first << "    " << pair5.second << std::endl;

    //6) 转发 first_args 的元素到 first 的构造函数并转发 second_args 的元素到 second 的构造函数。
    //这是能用于构造不可复制不可移动类型的 pair 的仅有的非默认构造函数。
    std::pair<int, Cell> pair6(301, {307, 308});
    std::cout << "pair6:" << std::setw(8) << pair6.first << "    " << pair6.second << std::endl;

    //7) 复制构造函数为默认,且若两个元素的复制满足 constexpr 函数的要求则为 constexpr 。
    std::pair<int, Cell> pair7(pair5);
    std::cout << "pair7:" << std::setw(8) << pair7.first << "    " << pair7.second << std::endl;

    //8) 移动构造函数为默认,且若两个元素的移动满足 constexpr 函数的要求则为 constexpr 。
    std::pair<int, Cell> pair8(std::move(pair6));
    std::cout << "pair8:" << std::setw(8) << pair8.first << "    " << pair8.second << std::endl;

    return 0;
}

输出

pair1:       0    {0,0}
pair2:     101    {102,103}
pair3:     201    {202,203}
pair4:     501    {502,503}
pair5:     601    {602,603}
pair6:     301    {307,308}
pair7:     601    {602,603}
pair8:     301    {307,308}

  • 19
    点赞
  • 18
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值