c++11 标准模板(STL)(std::pair)(七)访问 pair 的一个元素

定义于头文件 <utility>

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

访问 pair 的一个元素

复制代码
std::get(std::pair)

|---------------------------------------------------------------------------------------------------------------------------------------------------------------|-----|---------------------|---------------------|
| template< size_t I, class T1, class T2 > typename std::tuple_element<I, std::pair<T1,T2> >::type& get( pair<T1, T2>& p ) noexcept; | (1) | (C++11 起) (C++14 前) |
| template< size_t I, class T1, class T2 > constexpr std::tuple_element_t<I, std::pair<T1,T2> >& get( pair<T1, T2>& p ) noexcept; | (1) | (C++14 起) |
| template< size_t I, class T1, class T2 > const typename std::tuple_element<I, std::pair<T1,T2> >::type& get( const pair<T1,T2>& p ) noexcept; | (2) | (C++11 起) (C++14 前) |
| template< size_t I, class T1, class T2 > constexpr const std::tuple_element_t<I, std::pair<T1,T2> >& get( const pair<T1,T2>& p ) noexcept; | (2) | (C++14 起) |
| template< size_t I, class T1, class T2 > typename std::tuple_element<I, std::pair<T1,T2> >::type&& get( std::pair<T1,T2>&& p ) noexcept; | (2) | (3) | (C++11 起) (C++14 前) |
| template< size_t I, class T1, class T2 > constexpr std::tuple_element_t<I, std::pair<T1,T2> >&& get( std::pair<T1,T2>&& p ) noexcept; | (2) | (3) | (C++14 起) |
| template< size_t I, class T1, class T2 > constexpr const std::tuple_element_t<I, std::pair<T1,T2> >&& get( const std::pair<T1,T2>&& p ) noexcept; | (2) | (4) | (C++17 起) |
| template <class T, class U> constexpr T& get(std::pair<T, U>& p) noexcept; | (2) | (5) | (C++14 起) |
| template <class T, class U> constexpr const T& get(const std::pair<T, U>& p) noexcept; | (2) | (6) | (C++14 起) |
| template <class T, class U> constexpr T&& get(std::pair<T, U>&& p) noexcept; | (2) | (7) | (C++14 起) |
| template <class T, class U> constexpr const T&& get(const std::pair<T, U>&& p) noexcept; | (2) | (8) | (C++17 起) |
| template <class T, class U> constexpr T& get(std::pair<U, T>& p) noexcept; | (2) | (9) | (C++14 起) |
| template <class T, class U> constexpr const T& get(const std::pair<U, T>& p) noexcept; | (2) | (10) | (C++14 起) |
| template <class T, class U> constexpr T&& get(std::pair<U, T>&& p) noexcept; | (2) | (11) | (C++14 起) |
| template <class T, class U> constexpr const T&& get(const std::pair<U, T>&& p) noexcept; | (2) | (12) | (C++17 起) |
| (2) |
| (2) |
| (2) |
| (2) |
| (2) |
| (2) |
| (2) |
| (2) |
| (2) |
| (2) |
| (2) |
| (2) |
| (2) |
| (2) |
| (2) |
| (2) |
| (2) |
| (2) |
| (2) |
| (2) |
| (2) |
| (2) |
| (2) |
| (2) |
| (2) |
| (2) |
| (2) |
| (2) |
| (2) |
| (2) |
| (2) |
| (2) |
| (2) |
| (2) |
| (2) |
| (2) |
| (2) |
| (2) |
| (2) |
| (2) |
| (2) |
| (2) |
| (2) |
| (2) |
| (2) |
| (2) |
| (2) |
| (2) |
| (2) |
| (2) |
| (2) |
| (2) |
| (2) |
| (2) |
| (2) |
| (2) |
| (2) |
| (2) |
| (2) |
| (2) |
| (2) |
| (2) |
| (2) |
| (2) |
| (2) |
| (2) |
| (2) |
| (2) |
| (2) |
| (2) |
| (2) |
| (2) |
| (2) |
| (2) |
| (2) |
| (2) |
| (2) |
| (2) |
| (2) |
| (2) |
| (2) |
| (2) |
| (2) |
| (2) |
| (2) |
| (2) |
| (2) |
| (2) |
| (2) |
| (2) |
| (2) |
| (2) |
| (2) |
| (2) |
| (2) |
| (2) |
| (2) |
| (2) |
| (2) |
| (2) |
| (2) |
| (2) |
| (2) |
| (2) |
| (2) |
| (2) |
| (2) |
| (2) |
| (2) |
| (2) |
| (2) |
| (2) |
| (2) |
| (2) |
| (2) |
| (2) |
| (2) |
| (2) |
| (2) |
| (2) |
| (2) |
| (2) |
| (2) |
| (2) |
| (2) |
| (2) |
| (2) |

用类 tuple 的接口从 pair 提取元素。

若序号 I 不是 0 或 1 则基于范围的重载 (1-4) 无法编译。

若类型 TU 相同则基于类型的重载 (5-12) 无法编译。

参数

|---|---|-------------|
| p | - | 要提取内容的 pair |

返回值

1-4) 若 I==0 则返回到 p.first 的引用,若 I==1 则返回到 p.second 的引用。

5-8) 返回到 p.first 的引用。

9-12) 返回到 p.second 的引用。

调用示例

复制代码
#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) {}
    bool operator ==(const Cell &cell) const
    {
        return x == cell.x && y == cell.y;
    }

    bool operator <(const Cell &cell) const
    {
        if (x < cell.x)
        {
            return true;
        }
        return y < cell.y;
    }
};

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

std::ostream &operator<<(std::ostream &os, const std::pair<int, Cell> &pair)
{
    os << "pair{" << pair.first << " {" << pair.second.x << "," << pair.second.y << "}}";
    return os;
}

int main()
{
    std::pair<int, Cell> pair1(101, Cell(102, 103));
    std::cout << "pair1:" << std::setw(8) << std::get<0>(pair1) << "    "
              << std::get<1>(pair1) << std::endl;
    std::pair<int, Cell> pair2(101, Cell(102, 103));
    std::cout << "pair2:" << std::setw(8) << std::get<0>(std::move(pair2)) << "    "
              << std::get<1>(std::move(pair2)) << std::endl;

    return 0;
}

输出

复制代码
pair1:     101    {102,103}
pair2:     101    {102,103}
相关推荐
吴_知遇35 分钟前
【华为OD机试真题】428、连续字母长度 | 机试真题+思路参考+代码解析(E卷)(C++)
开发语言·c++·华为od
LaoWaiHang1 小时前
MFC案例:使用键盘按键放大、缩小窗口图像的实验
c++·mfc
到底怎么取名字不会重复1 小时前
Day10——LeetCode15&560
c++·算法·leetcode·哈希算法·散列表
陈大大陈2 小时前
基于 C++ 的用户认证系统开发:从注册登录到Redis 缓存优化
java·linux·开发语言·数据结构·c++·算法·缓存
纪元A梦2 小时前
华为OD机试真题——通过软盘拷贝文件(2025A卷:200分)Java/python/JavaScript/C++/C语言/GO六种最佳实现
java·javascript·c++·python·华为od·go·华为od机试题
刚入坑的新人编程3 小时前
C++多态
开发语言·c++
QUST-Learn3D3 小时前
高精度并行2D圆弧拟合(C++)
开发语言·c++
明月醉窗台3 小时前
Qt 入门 6 之布局管理
c语言·开发语言·c++·qt
云小逸4 小时前
【C++】继承
开发语言·c++
努力学习的小廉4 小时前
【C++】 —— 笔试刷题day_21
开发语言·c++·算法