std::pair<T1,T2>::pair
pair();
|
(1) | (自 C++11 起为 constexpr) (自 C++11 起有条件地为 explicit) |
pair( const T1& x, const T2& y );
|
(2) | (自 C++14 起为 constexpr) (自 C++11 起有条件地为 explicit) |
template< class U1, class U2 >
pair( U1&& x, U2&& y );
|
(3) | (自 C++11 起) (自 C++14 起为 constexpr) (直到 C++23) (有条件地为 explicit) |
template< class U1 = T1, class U2 = T2 >
constexpr pair( U1&& x, U2&& y );
|
(自 C++23 起) (有条件地为 explicit) |
|
template< class U1, class U2 >
constexpr pair( pair<U1, U2>& p );
|
(4) | (自 C++23 起) (有条件地为 explicit) |
template< class U1, class U2 >
pair( const pair<U1, U2>& p );
|
(5) | (自 C++14 起为 constexpr) (自 C++11 起有条件地为 explicit) |
template< class U1, class U2 >
pair( pair<U1, U2>&& p );
|
(6) | (自 C++14 起为 constexpr) (自 C++11 起有条件地为 explicit) |
template< class U1, class U2 >
constexpr pair( const pair<U1, U2>&& p );
|
(7) | (自 C++23 起) (有条件地为 explicit) |
template< pair-like P >
constexpr pair ( P&& u );
|
(8) | (自 C++23 起) (有条件地为 explicit) |
template< class... Args1, class... Args2 >
pair( std::piecewise_construct_t,
std::tuple<Args1...> first_args,
std::tuple<Args2...> second_args );
|
(9) | (自 C++11 起) (自 C++20 起为 constexpr) |
pair( const pair& p ) = default;
|
(10) | |
pair( pair&& p ) = default;
|
(11) | (自 C++11 起) |
构造一个新的 pair 对象。
first 和 second。
|
仅当 当且仅当 |
(自 C++11 起) |
first 初始化 x,使用 second 初始化 y。
|
仅当 当且仅当 |
(自 C++11 起) |
first 初始化 std::forward<U1>(x),使用 second 初始化 std::forward<U2>(y)。std::is_constructible_v<T1, U1> 和 std::is_constructible_v<T2, U2> 都是 true 时,此构造函数才参与重载决议。explicit 是 std::is_convertible_v<U1, T1> 或 false 是 std::is_convertible_v<U2, T2> 时,此构造函数是 false。
|
如果 |
(自 C++23 起) |
first 初始化 p.first,使用 second 初始化 p.second。std::is_constructible_v<T1, U1&> 和 std::is_constructible_v<T2, U2&> 都是 true 时,此构造函数才参与重载决议。explicit 是 std::is_convertible_v<U1&, T1> 或 false 是 std::is_convertible_v<U2&, T2> 时,此构造函数是 false。first 或 second 的初始化会将引用绑定到临时对象,则此构造函数被定义为弃置的。first 初始化 p.first,使用 second 初始化 p.second。
|
仅当 当且仅当 |
(自 C++11 起) |
|
如果 |
(自 C++23 起) |
first 初始化 std::forward<U1>(p.first),使用 second 初始化 std::forward<U2>(p.second)。std::is_constructible_v<T1, U1> 和 std::is_constructible_v<T2, U2> 都是 true 时,此构造函数才参与重载决议。explicit 是 std::is_convertible_v<U1, T1> 或 false 是 std::is_convertible_v<U2, T2> 时,此构造函数是 false。
|
如果 |
(自 C++23 起) |
first 初始化 std::forward<const U1>(p.first),使用 second 初始化 std::forward<const U2>(p.second)。std::is_constructible_v<T1, U1> 和 std::is_constructible_v<T2, U2> 都是 true 时,此构造函数才参与重载决议。explicit 是 std::is_convertible_v<const U1, T1> 或 false 是 std::is_convertible_v<const U2, T2> 时,此构造函数是 false。first 或 second 的初始化会将引用绑定到临时对象,则此构造函数被定义为弃置的。u1 为 std::get<0>(std::forward(u)),u2 为 std::get<1>(std::forward(u)),记它们的类型分别为 U1 和 U2。使用 first 初始化 u1,使用 second 初始化 u2。std::remove_cvref(P)不是 std::ranges::subrange 的特化,std::is_constructible_v<T1, U1>是true,且std::is_constructible_v<T2, U2是true。
explicit 是 std::is_convertible_v<U1, T1> 或 false 是 std::is_convertible_v<U2, T2> 时,此构造函数是 false。first 或 second 的初始化会将引用绑定到临时对象,则此构造函数被定义为弃置的。first_args 的元素转发给 first 的构造函数,并将 second_args 的元素转发给 second 的构造函数。这是唯一可用于创建不可复制不可移动类型的 pair 的非默认构造函数。如果 first 或 second 是引用类型并绑定到临时对象,则程序非良构。constexpr(自 C++11 起)。constexpr。参数
| x | - | 用于初始化该pair第一个元素的值 |
| y | - | 用于初始化该pair第二个元素的值 |
| p | - | 用于初始化该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) 值初始化: ", p1); std::pair<int, double> p2{42, 3.1415}; print("(2) 使用两个值初始化: ", p2); std::pair<char, int> p4{p2}; print("(4) 隐式转换: ", 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) 分段构造: ", p6); }
可能的输出:
(1) 值初始化: (0, 0) (2) 使用两个值初始化: (42, 3.1415) (4) 隐式转换: (*, 3) (8) 分段构造: ((0.123,7.7), aaaaaaaaaa)
缺陷报告
下列行为变更缺陷报告被追溯应用于先前发布的 C++ 标准。
| 缺陷报告 | 适用标准 | 发布时的行为 | 正确行为 |
|---|---|---|---|
| LWG 265 | C++98 |
默认构造函数通过
T1
(
)
和
T2
(
)
分别对
first
和
second
进行拷贝初始化
(因此要求
T1
和
T2
必须满足
可复制构造
要求)
|
first
和
second
进行值初始化 |
| LWG 2510 | C++11 | 默认构造函数为隐式 | 改为有条件显式 |
| N4387 | C++11 | 部分构造函数仅支持隐式转换,限制了某些使用场景 | 构造函数改为有条件显式 |
参见
根据参数类型创建
pair
对象
(函数模板) |
|
构造新的
tuple
(
std::tuple<Types...>
的公开成员函数)
|