当前位置: 首页 > 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}

相关文章:

  • 【华为OD题库-110】反转每对括号间的子串-java
  • Promise,async和js的事件循环机制
  • FPFA.一种二倍频电路代码描述以及测量详情
  • jar混淆,防止反编译,Allatori工具混淆jar包
  • springboot对接WebSocket实现消息推送
  • SpringBoot 3 集成Hive 3
  • 第十五节TypeScript 接口
  • 【MySQL】:超详细MySQL完整安装和配置教程
  • 【网络编程】基于UDP数据报实现回显服务器程序
  • 沉浸式go-cache源码阅读!
  • pytest 的 fixture 固件机制
  • 竞赛保研 基于RSSI的室内wifi定位系统
  • STM32软硬件CRC测速对比
  • Django之按钮(actions)
  • Linux服务器 部署飞书信息发送服务
  • [分享]iOS开发-关于在xcode中引用文件夹右边出现问号的解决办法
  • 2017年终总结、随想
  • conda常用的命令
  • ERLANG 网工修炼笔记 ---- UDP
  • Java 多线程编程之:notify 和 wait 用法
  • JavaScript新鲜事·第5期
  • Java深入 - 深入理解Java集合
  • java小心机(3)| 浅析finalize()
  • LintCode 31. partitionArray 数组划分
  • magento2项目上线注意事项
  • Netty 框架总结「ChannelHandler 及 EventLoop」
  • php ci框架整合银盛支付
  • Quartz初级教程
  • spring boot 整合mybatis 无法输出sql的问题
  • 从零搭建Koa2 Server
  • 从零开始在ubuntu上搭建node开发环境
  • 分享几个不错的工具
  • 前端之React实战:创建跨平台的项目架构
  • 如何解决微信端直接跳WAP端
  • 如何优雅的使用vue+Dcloud(Hbuild)开发混合app
  • 数据结构java版之冒泡排序及优化
  • 智能合约Solidity教程-事件和日志(一)
  • # 安徽锐锋科技IDMS系统简介
  • # 日期待t_最值得等的SUV奥迪Q9:空间比MPV还大,或搭4.0T,香
  • #pragma data_seg 共享数据区(转)
  • #我与Java虚拟机的故事#连载12:一本书带我深入Java领域
  • (02)Hive SQL编译成MapReduce任务的过程
  • (4)事件处理——(7)简单事件(Simple events)
  • (六)激光线扫描-三维重建
  • (算法)N皇后问题
  • (原創) 物件導向與老子思想 (OO)
  • (转)Scala的“=”符号简介
  • .Net Core缓存组件(MemoryCache)源码解析
  • .NET Core日志内容详解,详解不同日志级别的区别和有关日志记录的实用工具和第三方库详解与示例
  • .net mvc 获取url中controller和action
  • .NET 简介:跨平台、开源、高性能的开发平台
  • .net使用excel的cells对象没有value方法——学习.net的Excel工作表问题
  • /var/spool/postfix/maildrop 下有大量文件
  • [20150707]外部表与rowid.txt
  • [Angular] 笔记 8:list/detail 页面以及@Input