在线文档教程
C++
应用 | Utilities

std::pair::pair

STD::对:成对

(1)
pair((until C++11)
constexpr pair((since C++11) (until C++17)
/*EXPLICIT*/ constexpr pair((since C++17)
(2)
pair( const T1& x, const T2& y (until C++14)
constexpr pair( const T1& x, const T2& y (since C++14) (until C++17)
/*EXPLICIT*/ constexpr pair( const T1& x, const T2& y (since C++17)
(3)
template< class U1, class U2 > pair( U1&& x, U2&& y (since C++11) (until C++14)
template< class U1, class U2 > constexpr pair( U1&& x, U2&& y (since C++14) (until C++17)
template< class U1, class U2 > /*EXPLICIT*/ constexpr pair( U1&& x, U2&& y (since C++17)
(4)
template< class U1, class U2 > pair( const pair<U1, U2>& p (until C++14)
template< class U1, class U2 > constexpr pair( const pair<U1, U2>& p (since C++14) (until C++17)
template< class U1, class U2 > /*EXPLICIT*/ constexpr pair( const pair<U1, U2>& p (since C++17)
(5)
template< class U1, class U2 > pair( pair<U1, U2>&& p (since C++11) (until C++14)
template< class U1, class U2 > constexpr pair( pair<U1, U2>&& p (since C++14) (until C++17)
template< class U1, class U2 > /*EXPLICIT*/ constexpr pair( pair<U1, U2>&& p (since C++17)
template< class... Args1, class... Args2 > pair( std::piecewise_construct_t, std::tuple<Args1...> first_args, std::tuple<Args2...> second_args (6)(since C++11)
pair( const pair& p ) = default;(7)
pair( pair&& p ) = default;(8)(since C++11)

构造一个新的一对。

1%29默认构造函数。值-初始化对的两个元素,firstsecond...

This constructor participates in overload resolution if and only if std::is_default_constructible_v and std::is_default_constructible_v are both true. This constructor is explicit if and only if either first_type or second_type is not implicitly default-constructible.(since C++17)

  • 此构造函数参与过载解析的当且仅当std::is_default_constructible_v<first_type>和std::is_default_constructible_v<second_type>都是true...

  • 此构造函数是explicit当且仅当first_typesecond_type不可隐式默认-可构造。

%28自C++17%29

2%29初始化first带着xsecond带着y...

This constructor participates in overload resolution if and only if std::is_copy_constructible_v and std::is_copy_constructible_v are both true. This constructor is explicit if and only if std::is_convertible_v is false or std::is_convertible_v is false.(since C++17)

  • 此构造函数参与过载解析的当且仅当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...

%28自C++17%29

3%29初始化first带着std::forward<U1>(x)和second带着std::forward<U2>(y)...

This constructor participates in overload resolution only if U1 is implicitly convertible to first_type and U2 is implicitly convertible to second_type.(until C++17)
This constructor participates in overload resolution if and only if std::is_constructible_v<first_type, U1&&> and std::is_constructible_v<second_type, U2&&> are both true. This constructor is explicit if and only if std::is_convertible_v<U1&&, first_type> is false or std::is_convertible_v<U2&&, second_type> is false.(since C++17)

  • 只有在下列情况下,此构造函数才参与重载解析。U1隐式可转换为first_typeU2隐式可转换为second_type...%28直到C++17%29

  • 此构造函数参与过载解析的当且仅当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...

%28自C++17%29

4%29初始化first带着p.firstsecond带着p.second...

This constructor participates in overload resolution if and only if std::is_constructible_v and std::is_constructible_v are both true. This constructor is explicit if and only if std::is_convertible_v is false or std::is_convertible_v is false.(since C++17)

  • 此构造函数参与过载解析的当且仅当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...

%28自C++17%29

5%29初始化first带着std::forward<U1>(p.first)和second带着std::forward<U2>(p.second)...

This constructor participates in overload resolution if and only if std::is_constructible_v and std::is_constructible_v are both true. This constructor is explicit if and only if std::is_convertible_v is false or std::is_convertible_v is false.(since C++17)

  • 此构造函数参与过载解析的当且仅当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...

%28自C++17%29

6%29转发first_args的构造函数first的元素second_args的构造函数second这是唯一可以用来创建一对不可复制的不可移动类型的非默认构造函数。

7%29复制构造函数是默认的,并且是constexpr如果两个元素的复制都满足对conexpr函数的要求。

8%29移动构造函数是默认的,并且是constexpr如果两个元素的运动满足对常数函数的要求。

参数

x-value to initialize the first element of this pair
y-value to initialize the second element of this pair
p-pair of values used to initialize both elements of this pair
first_args-tuple of constructor arguments to initialize the first element of this pair
second_args-tuple of constructor arguments to initialize the second element of this pair

例外

不引发异常,除非指定的操作之一为%28例如。元素%29抛出的构造函数。

二次

#include <utility> #include <string> #include <complex> #include <tuple> #include <iostream> int main() { std::pair<int, float> p1; std::cout << "Value-initialized: " << p1.first << ", " << p1.second << '\n'; std::pair<int, double> p2(42, 0.123 std::cout << "Initialized with two values: " << p2.first << ", " << p2.second << '\n'; std::pair<char, int> p4(p2 std::cout << "Implicitly converted: " << p4.first << ", " << p4.second << '\n'; 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') std::cout << "Piecewise constructed: " << p6.first << ", " << p6.second << '\n'; }

二次

产出:

二次

Value-initialized: 0, 0 Initialized with two values: 42, 0.123 Implicitly converted: *, 0 Piecewise constructed: (0.123,7.7), aaaaaaaaaa

二次

另见

make_paircreates a pair object of type, defined by the argument types (function template)

© cppreference.com

在CreativeCommonsAttribution下授权-ShareAlike未移植许可v3.0。

http://en.cppreference.com/w/cpp/实用程序/对/对