std::pair::pair
cppreference.com
<tbody>
</tbody>
<tbody class="t-dcl-rev t-dcl-rev-num ">
</tbody><tbody>
</tbody>
<tbody class="t-dcl-rev t-dcl-rev-num ">
</tbody><tbody>
</tbody>
<tbody class="t-dcl-rev t-dcl-rev-num ">
</tbody><tbody>
</tbody>
<tbody class="t-dcl-rev t-dcl-rev-num ">
</tbody><tbody>
</tbody>
<tbody class="t-dcl-rev t-dcl-rev-num ">
</tbody><tbody>
</tbody>
<tbody class="t-dcl-rev t-dcl-rev-num ">
</tbody><tbody>
</tbody>
| (1) | ||
pair(); |
( C++11) | |
constexpr pair(); |
( C++11) (conditionally explicit) |
|
| (2) | ||
pair( const T1& x, const T2& y ); |
( C++11) | |
pair( const T1& x, const T2& y ); |
( C++11) ( C++14) (conditionally explicit) |
|
constexpr pair( const T1& x, const T2& y ); |
( C++14) (conditionally explicit) |
|
| (3) | ||
template< class U1, class U2 > pair( U1&& x, U2&& y ); |
( C++11) ( C++14) (conditionally explicit) |
|
template< class U1, class U2 > constexpr pair( U1&& x, U2&& y ); |
( C++14) ( C++23) (conditionally explicit) |
|
template< class U1 = T1, class U2 = T2 > constexpr pair( U1&& x, U2&& y ); |
( C++23) (conditionally explicit) |
|
template< class U1, class U2 > constexpr pair( pair<U1, U2>& p ); |
(4) | ( C++23) (conditionally explicit) |
| (5) | ||
template< class U1, class U2 > pair( const pair<U1, U2>& p ); |
( C++11) | |
template< class U1, class U2 > pair( const pair<U1, U2>& p ); |
( C++11) ( C++14) (conditionally explicit) |
|
template< class U1, class U2 > constexpr pair( const pair<U1, U2>& p ); |
( C++14) (conditionally explicit) |
|
| (6) | ||
template< class U1, class U2 > pair( pair<U1, U2>&& p ); |
( C++11) ( C++14) (conditionally explicit) |
|
template< class U1, class U2 > constexpr pair( pair<U1, U2>&& p ); |
( C++14) (conditionally explicit) |
|
template< class U1, class U2 > constexpr pair( const pair<U1, U2>&& p ); |
(7) | ( C++23) (conditionally explicit) |
template< pair-like P > constexpr pair ( P&& u ); |
(8) | ( C++23) (conditionally explicit) |
| (9) | ||
template< class... Args1, class... Args2 > pair( std::piecewise_construct_t, std::tuple<Args1...> first_args, std::tuple<Args2...> second_args ); |
( 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; |
(10) | |
pair( pair&& p ) = default; |
(11) | ( C++11) |
pair.
1) . pair,
first second.
|
, |
( C++11) |
2)
first x second y.
|
, |
( C++11) |
3)
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) |
4)
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 .5)
first p.first second p.second.
|
, |
( C++11) |
|
, |
( C++23) |
6)
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) |
7)
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 .8)
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, U2true.
explicit , std::is_convertible_v<U1, T1> false std::is_convertible_v<U2, T2> false. ,
first second .9)
first_args first second_args second. , . , first second .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) : ", 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 | first second T1() T2() ( , T1 T2 CopyConstructible) |
first second
|
| LWG 2510 | C++11 | - | |
| WG | C++11 | , |
- |
pair, ( ) | |
tuple (public - std::tuple)
|