首页
学习
活动
专区
工具
TVP
发布
精选内容/技术社群/优惠产品,尽在小程序
立即前往

std::make_pair

Defined in header <utility>

template< class T1, class T2 > std::pair<T1,T2> make_pair( T1 t, T2 u );

(until C++11)

template< class T1, class T2 > std::pair<V1,V2> make_pair( T1&& t, T2&& u );

(since C++11) (until C++14)

template< class T1, class T2 > constexpr std::pair<V1,V2> make_pair( T1&& t, T2&& u );

(since C++14)

创建std::pair对象,从参数类型推断目标类型。

The deduced types V1 and V2 are std::decay<T1>::type and std::decay<T2>::type (the usual type transformations applied to arguments of functions passed by value) unless application of std::decay results in std::reference_wrapper<X> for some type X, in which case the deduced type is X&.

(since C++11)

参数

t, u

-

the values to construct the pair from

返回值

std::pair对象,其中包含给定的值。

二次

代码语言:javascript
复制
#include <iostream>
#include <utility>
#include <functional>
 
int main()
{
    int n = 1;
    int a[5] = {1, 2, 3, 4, 5};
 
    // build a pair from two ints
    auto p1 = std::make_pair(n, a[1]);
    std::cout << "The value of p1 is "
              << "(" << p1.first << ", " << p1.second << ")\n";
 
    // build a pair from a reference to int and an array (decayed to pointer)
    auto p2 = std::make_pair(std::ref(n), a);
    n = 7;
    std::cout << "The value of p2 is "
              << "(" << p2.first << ", " << *(p2.second + 2) << ")\n";
}

二次

产出:

二次

代码语言:javascript
复制
The value of p1 is (1, 2)
The value of p2 is (7, 3)

二次

代码语言:txt
复制
 © cppreference.com

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

扫码关注腾讯云开发者

领取腾讯云代金券