std::pair::pair
来自cppreference.com
该页由英文版wiki使用Google Translate机器翻译而来。
该翻译可能存在错误或用词不当。鼠标停留在文本上可以看到原版本。你可以帮助我们修正错误或改进翻译。参见说明请点击这里. |
pair(); constexpr pair(); |
(1) | (至 C++11) (C++11 起) |
pair( const T1& x, const T2& y ); |
(2) | |
template< class U1, class U2 > pair( U1&& x, U2&& y ); |
(3) | (C++11 起) |
template< class U1, class U2 > pair( const pair<U1,U2>& p ); |
(4) | |
template< class U1, class U2 > pair( pair<U1,U2>&& p ); |
(5) | (C++11 起) |
template< class... Args1, class... Args2 > pair( std::piecewise_construct_t, |
(6) | (C++11 起) |
pair( const pair& p ) = default; |
(7) | |
pair( pair&& p ) = default; |
(8) | (C++11 起) |
构造一个新的对.
1) 默认构造函数。值初始化的两个元素对,
2) first
和second
.原文:
Default constructor. Value-initializes both elements of the pair,
first
and second
.初始化
3)
4) first
与x
second
y
.原文:
Initializes
first
with x
and second
with y
.初始化
5)
6) first
与p.first
second
p.second
. 原文:
Initializes
first
with p.first
and second
with p.second
. 前锋
7) first_args
的构造函数first
的元素,并转发second_args
second
的构造函数中的元素。这是唯一的非默认构造函数,可以用来创建一个对不可复制的非动产类型.原文:
Forwards the elements of
first_args
to the constructor of first
and forwards the elements of second_args
to the constructor of second
. This is the only non-default constructor that can be used to create a pair of non-copyable non-movable types.拷贝构造函数是隐式产生的
8) 原文:
Copy constructor is implicitly generated.
移动构造函数隐式产生的
原文:
Move constructor is implicitly generated.
[编辑] 参数
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 |
[编辑] 示例
#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
[编辑] 另请参阅
创建一个根据参数类型所定义类型的pair 对象 (函数模板) |