命名空间
变体
操作

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

来自 cppreference.cn
< cpp‎ | utility‎ | pair
 
 
 
 
pair();
(1) (constexpr since C++11)
(conditionally explicit since C++11)
pair( const T1& x, const T2& y );
(2) (conditionally explicit since C++11)
(constexpr since C++14)
(3)
template< class U1, class U2 >
pair( U1&& x, U2&& y );
(since C++11)
(until C++23)
(constexpr since C++14)
(conditionally explicit)
template< class U1 = T1, class U2 = T2 >
constexpr pair( U1&& x, U2&& y );
(since C++23)
(conditionally explicit)
template< class U1, class U2 >
constexpr pair( pair<U1, U2>& p );
(4) (since C++23)
(conditionally explicit)
template< class U1, class U2 >
pair( const pair<U1, U2>& p );
(5) (conditionally explicit since C++11)
(constexpr since C++14)
template< class U1, class U2 >
pair( pair<U1, U2>&& p );
(6) (constexpr since C++14)
(conditionally explicit since C++11)
template< class U1, class U2 >
constexpr pair( const pair<U1, U2>&& p );
(7) (since C++23)
(conditionally explicit)
template< pair-like P >
constexpr pair ( P&& u );
(8) (since C++23)
(conditionally explicit)
template< class... Args1, class... Args2 >

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

      std::tuple<Args2...> second_args );
(9) (since C++11)
(constexpr since C++20)
pair( const pair& p ) = default;
(10)
pair( pair&& p ) = default;
(11) (since C++11)

构造一个新的 pair。

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

当且仅当 std::is_default_constructible_v<T1>std::is_default_constructible_v<T2> 均为 true 时,此构造函数参与重载决议。

当且仅当 T1T2 不是隐式默认可构造时,此构造函数是 explicit 的。

(since C++11)
2) 使用 x 初始化 first,使用 y 初始化 second

当且仅当 std::is_copy_constructible_v<T1>std::is_copy_constructible_v<T2> 均为 true 时,此构造函数参与重载决议。

当且仅当 std::is_convertible_v<const T1&, T1>falsestd::is_convertible_v<const T2&, T2>false 时,此构造函数是 explicit 的。

(since C++11)
3) 使用 std::forward<U1>(x) 初始化 first,使用 std::forward<U2>(y) 初始化 second
当且仅当 std::is_constructible_v<T1, U1>std::is_constructible_v<T2, U2> 均为 true 时,此构造函数参与重载决议。
当且仅当 std::is_convertible_v<U1, T1>falsestd::is_convertible_v<U2, T2>false 时,此构造函数是 explicit 的。

如果 firstsecond 的初始化会将引用绑定到临时对象,则此构造函数被定义为已删除。

(since C++23)
4) 使用 p.first 初始化 first,使用 p.second 初始化 second
当且仅当 std::is_constructible_v<T1, U1&>std::is_constructible_v<T2, U2&> 均为 true 时,此构造函数参与重载决议。
当且仅当 std::is_convertible_v<U1&, T1>falsestd::is_convertible_v<U2&, T2>false 时,此构造函数是 explicit 的。
如果 firstsecond 的初始化会将引用绑定到临时对象,则此构造函数被定义为已删除。
5) 使用 p.first 初始化 first,使用 p.second 初始化 second

当且仅当 std::is_constructible_v<T1, const U1&>std::is_constructible_v<T2, const U2&> 均为 true 时,此构造函数参与重载决议。

当且仅当 std::is_convertible_v<const U1&, T1>falsestd::is_convertible_v<const U2&, T2>false 时,此构造函数是 explicit 的。

(since C++11)

如果 firstsecond 的初始化会将引用绑定到临时对象,则此构造函数被定义为已删除。

(since C++23)
6) 使用 std::forward<U1>(p.first) 初始化 first,使用 std::forward<U2>(p.second) 初始化 second
当且仅当 std::is_constructible_v<T1, U1>std::is_constructible_v<T2, U2> 均为 true 时,此构造函数参与重载决议。
当且仅当 std::is_convertible_v<U1, T1>falsestd::is_convertible_v<U2, T2>false 时,此构造函数是 explicit 的。

如果 firstsecond 的初始化会将引用绑定到临时对象,则此构造函数被定义为已删除。

(since C++23)
7) 使用 std::forward<const U1>(p.first) 初始化 first,使用 std::forward<const U2>(p.second) 初始化 second
当且仅当 std::is_constructible_v<T1, U1>std::is_constructible_v<T2, U2> 均为 true 时,此构造函数参与重载决议。
当且仅当 std::is_convertible_v<const U1, T1>falsestd::is_convertible_v<const U2, T2>false 时,此构造函数是 explicit 的。
如果 firstsecond 的初始化会将引用绑定到临时对象,则此构造函数被定义为已删除。
8) 给定 u1std::get<0>(std::forward(u))u2std::get<1>(std::forward(u)),将其类型分别表示为 U1U2。使用 u1 初始化 first,使用 u2 初始化 second
当且仅当以下条件成立时,此构造函数参与重载决议:
当且仅当 std::is_convertible_v<U1, T1>falsestd::is_convertible_v<U2, T2>false 时,此构造函数是 explicit 的。
如果 firstsecond 的初始化会将引用绑定到临时对象,则此构造函数被定义为已删除。
9)first_args 的元素转发到 first 的构造函数,并将 second_args 的元素转发到 second 的构造函数。这是唯一可用于创建不可复制、不可移动类型的 pair 的非默认构造函数。如果 firstsecond 是引用并绑定到临时对象,则程序是非良构的。
10) 复制构造函数是隐式声明的(直到 C++11)默认的,并且如果两个元素的复制都满足 constexpr 函数的要求,则为 constexpr(自 C++11 起)
11) 移动构造函数是默认的,并且如果两个元素的移动都满足 constexpr 函数的要求,则为 constexpr

内容

[编辑] 参数

x - 用于初始化此 pair 的第一个元素的值
y - 用于初始化此 pair 的第二个元素的值
p - 用于初始化此 pair 的两个元素的 pair 值
u - pair-like 值对象,用于初始化此 pair 的两个元素
first_args - 用于初始化此 pair 的第一个元素的构造函数参数元组
second_args - 用于初始化此 pair 的第二个元素的构造函数参数元组

[编辑] 异常

除非指定的某个操作(例如,元素的构造函数)抛出异常,否则不抛出异常。

[编辑] 示例

#include <complex>
#include <iostream>
#include <string>
#include <tuple>
#include <utility>
 
int main()
{
    auto print = [](auto rem, auto const& pair)
    {
        std::cout << rem << "(" << pair.first << ", " << pair.second << ")\n";
    };
 
    std::pair<int, float> p1;
    print("(1) Value-initialized: ", p1);
 
    std::pair<int, double> p2{42, 3.1415};
    print("(2) Initialized with two values: ", p2);
 
    std::pair<char, int> p4{p2};
    print("(4) Implicitly converted: ", p4);
 
    std::pair<std::complex<double>, std::string> p6
        {std::piecewise_construct, std::forward_as_tuple(0.123, 7.7),
            std::forward_as_tuple(10, 'a')};
    print("(8) Piecewise constructed: ", p6);
}

可能的输出

(1) Value-initialized: (0, 0)
(2) Initialized with two values: (42, 3.1415)
(4) Implicitly converted: (*, 3)
(8) Piecewise constructed: ((0.123,7.7), aaaaaaaaaa)

[编辑] 缺陷报告

以下行为变更缺陷报告被追溯应用于先前发布的 C++ 标准。

DR 应用于 已发布行为 正确行为
LWG 265 C++98 默认构造函数拷贝初始化 first
second 分别使用 T1()T2()
(因此要求 T1T2可复制构造 (CopyConstructible)
firstsecond
被值初始化
LWG 2510 C++11 默认构造函数是隐式的 设为有条件地显式
N4387 C++11 一些构造函数是仅隐式的,阻止了一些用法 构造函数设为有条件地显式

[编辑] 参见

创建类型由实参类型确定的 pair 对象
(函数模板) [编辑]
构造新的 tuple
(std::tuple<Types...> 的公共成员函数) [编辑]