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.mzph.cn/news/496228.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

python selenium 用法 和 Chrome headless

From: http://cuiqingcai.com/2599.html Selenium教程&#xff1a;https://www.yiibai.com/selenium selenium 官方参考文档&#xff1a;https://selenium-python.readthedocs.io/index.html Selenium Documentation&#xff1a;https://www.seleniumhq.org/docs Selenium 与 …

linux ssh 时间设置,Linux下设置SSH Server设置时间链接限制(示例代码)

OpenSSH基于安全的理由&#xff0c;如果用户连线到SSHServer后闲置一段时间&#xff0c;SSH Server会在超过特定时间后自动终止SSH连线。本人习惯长时间连接&#xff0c;需要做如下修改&#xff1a;1、打开ssh配置文件&#xff1a;# vim /etc/ssh/sshd_config加入如下两个参数保…

【itext学习之路】--1.创建一个简单的pdf文档

来源&#xff1a;https://blog.csdn.net/tomatocc/article/details/80666011 iText是著名的开放源码的站点sourceforge一个项目&#xff0c;是用于生成PDF文档的一个java类库。通过iText不仅可以生成PDF或rtf的文档&#xff0c;而且可以将XML、Html文件转化为PDF文件 本教程中…

redis linux服务,linux服务之redis

rediswget http://download.redis.io/releases/redis-4.0.6.tar.gzcd redis-4.0.6makecd src/./redis-server./redis-cli先启动server&#xff0c;再用cli去连接&#xff0c;开两个终端窗口[rootlocalhost src]# ./redis-cliCould not connect to Redis at 127.0.0.1:6379: Con…

并发服务器设计思路,参考apache学习UDP和QoS,研究成果

研究了快1个月的服务器架构&#xff0c;把研究成果记录一下。参考的有&#xff1a;Apache vlc ACE ftp我主要需要其中的并发处理&#xff0c;内存管理&#xff0c;TCP/UDP.QoS&#xff0c;速度限制等方面的内容&#xff0c;所以着重说这几方面。首先看一下Apache的基本框图&…

一文看懂NB-IoT!

来源&#xff1a;物联江湖(iot521) 作者&#xff1a;王一鸣一直以来&#xff0c;人们通过相应的终端&#xff08;电脑、手机、平板等&#xff09;使用网络服务&#xff0c;“个人”一直是网络的用户主体。个人对网络质量的要求“高”且“统一”&#xff1a;玩网络游戏必需要低…

安装、配置 Java JDK 和 JRE,并卸载自带 OpenJDK

JRE 和 JDK 的区别是什么&#xff1f;&#xff1a;https://www.zhihu.com/question/20317448 如何配置 Java 环境变量&#xff1a;https://jingyan.baidu.com/article/fd8044fa2c22f15031137a2a.html Windows 10 配置Java 环境变量&#xff1a;https://www.runoob.com/w3cnote…

软件测试面试题linux,linux基础面试题

该楼层疑似违规已被系统折叠 隐藏此楼查看此楼linux的用户管理useradd 用户名&#xff0c;添加用户【案例】useradd xiaomingpasswd 用户名&#xff0c;为新用户设密码【案例】passwd xiaoming&#xff0c;修改小明的密码userdel 用户名&#xff0c;删除用户【案例】userdel xi…

将十进制数转为N进制的方法

设有一个数 A,比如 A15 ,转为四进制数应当表示为33.原理如下:1.A除N,2.A模N.3.保存A模N的值。4.循环1&#xff0c;2。直到 A除N的值等于0;Codeclass NumerationConverter { /// <summary> /// 将十进制度数转为四进制 /// </summary> …

人工智能即将冲击与改变现有的医疗方式

来源呢&#xff1a;千家网从1960年代初&#xff0c;学术界陆续展开对于人工智能的研究&#xff0c;一直到目前的机器学习、深度学习等观念&#xff0c;所带来的第三波人工智能浪潮。对于医疗领域来说&#xff0c;在1970年代初期&#xff0c;人工智能就已经被应用在各项检查&…

【itext学习之路】--2.设置pdf的一些常用属性

来源&#xff1a;https://blog.csdn.net/tomatocc/article/details/80666361 在上一篇文章中&#xff0c;我们已经成功的创建了一个简单的pdf&#xff0c;下面我将学习设置该pdf的常用属性&#xff0c;其中包括&#xff1a;作者&#xff0c;创建时间&#xff0c;pdf创建者&…

简单好用的 Linux/Windows 面板

简单好用的 Linux/Windows 面板宝塔官网&#xff1a;https://www.bt.cn 宝塔Linux面板新手安装教程&#xff1a;https://www.cnblogs.com/paul8339/p/7065799.html https://blog.csdn.net/letterss/article/details/80216091 宝塔面板手册截图&#xff08;地址&#xff1a;http…

linux 串口读取陀螺仪,stm32读取陀螺仪MPU6050发送数据到串口

【实例简介】IAR环境下&#xff0c;stm32读取MPU6050数据&#xff0c;发送到串口。【实例截图】【核心代码】6b42b05e-a094-444f-b033-eda513b6cc49└── tly01├── Debug│ ├── Exe│ │ └── test.out│ └── Obj│ ├── analog_i2c.o│ ├── core…

任正非:中美领跑AI说法不合适、5G被炒作过热……

来源&#xff1a;羊城晚报 作者&#xff1a;宋毅摘要&#xff1a;2017年&#xff0c;华为又交出漂亮答卷&#xff1a;实现全球销售收入6036亿元&#xff0c;同比增长15.7%。4日&#xff0c;任正非接受了羊城晚报等5家媒体的采访&#xff0c;谈到了华为每年15%的研发投入&#…

ASP.NET (C#开发环境)Request对象 之 ServerVariables集合

Request.ServerVariables["HTTP_USER_AGENT"] <--> 返回浏览器类型和版本号 Request.ServerVariables["REMOTE_ADDR"] <--> 获取用户的IP地址 Request.ServerVariables["REQUEST_METHOD"] <--> 获取请求的方法 Request.Server…

【itext学习之路】--3.对pdf文档进行加密和权限设置

来源&#xff1a;https://blog.csdn.net/tomatocc/article/details/80667838 上篇文章&#xff0c;我们学习了pdf的属性设置&#xff0c;但是我们知道&#xff0c;在实际开发中&#xff0c;如果pdf文档被黑客盗取的话&#xff0c;那么pdf中的信息就会被泄露&#xff0c;因此本…

scrapy 模拟登陆

python 模拟登录豆瓣 并 发表动态&#xff1a;https://blog.csdn.net/freeking101/article/details/65445551 python网络爬虫之使用scrapy自动登录网站&#xff1a;https://www.cnblogs.com/zhanghongfeng/p/7684415.html Scrapy笔记&#xff08;11&#xff09;- 模拟登录&am…

李国杰院士等:未来移动通信系统中的通信与计算融合

来源 5G 作者&#xff1a;周一青 李国杰周一青&#xff1a;中国科学院大学教授&#xff0c;中国科学院计算技术研究所“百人计划”研究员、博导&#xff0c;无线通信技术研究中心副主任&#xff0c;移动计算与新型终端北京市重点实验室研究员。李国杰&#xff1a;中国工程院院…

ubuntu镜像源列表

Archive.ubuntu.com更新服务器&#xff08;欧洲&#xff0c;此为官方源&#xff0c;电信网通用户使用)&#xff1a; deb http://archive.ubuntu.com/ubuntu/ gutsy main restricted universe multiverse deb http://archive.ubuntu.com/ubuntu/ gutsy-security main restricted…

linux查看link 路径,link_path_walk()路径名查找

link_path_walk()路径名查找link_path_walk()函数。它接收的参数为要解析的路径名指针name和拥有目录项信息和安装文件系统信息的nameidata数据结构的地址nd&#xff0c;此时nd的path字段存放的是查找的路径名的基目录的路径。其定义如下&#xff1a;-------------------------…