当前位置: 首页 > news >正文

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;
(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;
(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;
(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;
(4)(C++17 起)

template <class T, class U>
constexpr T& get(std::pair<T, U>& p) noexcept;

(5)(C++14 起)

template <class T, class U>
constexpr const T& get(const std::pair<T, U>& p) noexcept;

(6)(C++14 起)

template <class T, class U>
constexpr T&& get(std::pair<T, U>&& p) noexcept;

(7)(C++14 起)

template <class T, class U>
constexpr const T&& get(const std::pair<T, U>&& p) noexcept;

(8)(C++17 起)

template <class T, class U>
constexpr T& get(std::pair<U, T>& p) noexcept;

(9)(C++14 起)

template <class T, class U>
constexpr const T& get(const std::pair<U, T>& p) noexcept;

(10)(C++14 起)

template <class T, class U>
constexpr T&& get(std::pair<U, T>&& p) noexcept;

(11)(C++14 起)

template <class T, class U>
constexpr const T&& get(const std::pair<U, T>&& p) noexcept;

(12)(C++17 起)

用类 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}

http://www.lryc.cn/news/267270.html

相关文章:

  • IP 地址归属地查询
  • 实战经验分享:在Java中灵活应用Excel注释和批注
  • AUTOSAR从入门到精通-车载以太网(三)
  • 【自然语言处理】用Python从文本中删除个人信息-第二部分
  • 设计模式之-中介者模式,快速掌握中介者模式,通俗易懂的讲解中介者模式以及它的使用场景
  • 12.25
  • MySQL5.7的几种安装方式总结(排错踩坑呕心沥血的经历)
  • zookeeper基本使用
  • 【华为机试】2023年真题B卷(python)-分月饼
  • EtherCAT主站SOEM -- 11 -- EtherCAT从站 XML 文件解析
  • YOLOv5算法改进(23)— 更换主干网络GhostNet + 添加CA注意力机制 + 引入GhostConv
  • centos系统部署rancher1.6版本并部署服务
  • Matlab实时读取串口数据并实时画图方法
  • 智能优化算法应用:基于向量加权平均算法3D无线传感器网络(WSN)覆盖优化 - 附代码
  • SpringBoot - Maven 打包合并一个胖 JAR 以及主项目 JAR 依赖 JAR 分离打包解决方案
  • react 18 Hooks扩展函数式组件的状态管理
  • 智能优化算法应用:基于浣熊算法3D无线传感器网络(WSN)覆盖优化 - 附代码
  • c++ qt QtWidgetsApplication 项目 使用外部ui
  • 使用React 18、Echarts和MUI实现温度计
  • 使用代码生成工具快速开发应用-结合后端Web API提供接口和前端页面快速生成,实现通用的业务编码规则管理
  • Android 13 - Media框架(26)- OMXNodeInstance(三)
  • 力扣题目学习笔记(OC + Swift)21. 合并两个有序链表
  • C# WPF上位机开发(windows pad上的应用)
  • Word使用技巧【开题报告】
  • 电子学会C/C++编程等级考试2022年06月(七级)真题解析
  • git中的smart checkout和force checkout
  • vue3整合Element-Plus,极速上手。
  • 学习Vue2.x
  • 新手如何快速熟悉代码,写出东西(持续更新)
  • 11-网络安全框架及模型-软件安全能力成熟度模型(SSCMM)