定义于头文件 <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 > | (3) | (C++11 起) (C++14 前) (条件性 explicit) |
| template< class U1, class U2 > | (C++14 起) (条件性 explicit) | |
| template< class U1, class U2 > | (4) | (C++11 前) |
| template< class U1, class U2 > | (C++11 起) (C++14 前) (条件性 explicit) | |
| template< class U1, class U2 > | (C++14 起) (条件性 explicit) | |
| template< class U1, class U2 > | (5) | (C++11 起) (C++14 前) (条件性 explicit) |
| template< class U1, class U2 > | (C++14 起) (条件性 explicit) | |
| template< class... Args1, class... Args2 > pair( std::piecewise_construct_t, | (6) | (C++11 起) (C++20 前) |
| template< class... Args1, class... Args2 > constexpr pair( std::piecewise_construct_t, | (C++20 起) | |
| pair( const pair& p ) = default; | (7) | |
| pair( pair&& p ) = default; | (8) | (C++11 起) |
构造新的 pair 。
1) 默认构造函数。值初始化 pair 的两个元素 first 和 second 。
| (C++11 起) |
2) 以 x 初始化 first 并以 y 初始化 second 。
| (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 。
| (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 | 应用于 | 出版时的行为 | 正确行为 |
|---|---|---|---|
| N4387 | C++11 | 某些构造函数曾为 explicit ,阻止了有用的行为 | 使大多数构造函数为条件性 explicit |
| LWG 2510 | C++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}