c++11 标准模板(STL)(std::pair)(五)创建一个 pair 对象,其类型根据各实参类型定义

定义于头文件 <utility>

std::pair 是一个结构体模板,其可于一个单元存储两个相异对象。 pair 是 std::tuple 的拥有两个元素的特殊情况。

创建一个 pair 对象,其类型根据各实参类型定义

std::make_pair

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

(C++11 前)

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

(C++11 起)
(C++14 前)

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

(C++14 起)

构造 std::pair 对象,从参数类型推导目标类型。

推导结果类型 V1V2 是 std::decay<T1>::type 与 std::decay<T2>::type (应用到按值传递的函数参数的通常类型变换),除非应用 std::decay 到某类型 X 产生 std::reference_wrapper<X> ,此情况下推导结果类型是 X&

(C++11 起)

参数

t, u - 构造 pair 所用的值

返回值

含有指定值的 std::pair 对象。

调用示例

#include <iostream>
#include <string>
#include <iomanip>
#include <complex>
#include <tuple>

struct Cell
{
    int x;
    int y;

    Cell() = default;
    Cell(int a, int b): x(a), y(b) {}
};

std::ostream &operator<<(std::ostream &os, const Cell &cell)
{
    os << "{" << cell.x << "," << cell.y << "}";
    return os;
}

int main()
{
    //构造 std::pair 对象,从参数类型推导目标类型。
    std::pair<int, Cell> pair1 = std::make_pair(101, Cell(102, 103));
    std::cout << "pair1:" << std::setw(8) << pair1.first << "    " << pair1.second << std::endl;
    std::pair<int, Cell> pair2 = std::make_pair(std::move(201), std::move(Cell(202, 203)));
    std::cout << "pair2:" << std::setw(8) << pair2.first << "    " << pair2.second << std::endl;

    auto pair3 = std::make_pair(301, Cell(302, 303));
    std::cout << "pair3:" << std::setw(8) << pair3.first << "    " << pair3.second << std::endl;
    auto pair4 = std::make_pair(std::move(401), std::move(Cell(402, 403)));
    std::cout << "pair4:" << std::setw(8) << pair4.first << "    " << pair4.second << std::endl;

    return 0;
}

 输出

pair1:     101    {102,103}
pair2:     201    {202,203}
pair3:     301    {302,303}
pair4:     401    {402,403}

获得pair的大小

std::tuple_size<std::pair>

template< class T1, class T2 >
struct tuple_size<std::pair<T1, T2>>;

(C++11 起)
(C++14 前)
template <class T1, class T2>

struct tuple_size<std::pair<T1, T2>>

  : std::integral_constant<std::size_t, 2> { };
(C++14 起)

 std::tuple_size 对 pair 的部分特化提供使用类 tuple 语法,在编译时获得 pair 中元素数的方法,该数总是 2 。

继承自 std::integral_constant

成员常量

value

[静态]

常数值 2
(公开静态成员常量)

成员函数

operator std::size_t

转换对象为 std::size_t ,返回 value
(公开成员函数)

operator()

(C++14)

返回 value
(公开成员函数)

成员类型

类型 定义
value_type std::size_t
type std::integral_constant<std::size_t, value>

 调用示例

#include <iostream>
#include <string>
#include <iomanip>
#include <complex>
#include <tuple>
#include <typeinfo>

struct Cell
{
    int x;
    int y;

    Cell() = default;
    Cell(int a, int b): x(a), y(b) {}
};

std::ostream &operator<<(std::ostream &os, const Cell &cell)
{
    os << "{" << cell.x << "," << cell.y << "}";
    return os;
}

template<class T>
void test(T t)
{
    int a[std::tuple_size<T>::value]; // 可用于编译时
    std::cout << typeid(t).name() << ": ";
    std::cout << std::tuple_size<T>::value << std::endl; // 或运行时
}

int main()
{
    test(std::make_tuple(1, 2, 3.14));
    test(std::make_pair(101, Cell(102, 103)));

    return 0;
}

输出

St5tupleIJiidEE: 3
St4pairIi4CellE: 2

猜你喜欢

转载自blog.csdn.net/qq_40788199/article/details/135038809