C++——pair用法总结

news2025/7/17 11:44:32

C++——pair用法总结

  • 1.pair概述(在标头 <utility> 定义)
  • 2.pair使用
    • 2.1成员函数(构造函数、赋值函数)
    • 2.2非成员函数
  • 2.3辅助类
  • 使用

1.pair概述(在标头 定义)

std::pair 是类模板,提供在一个单元存储两个相异类型对象的途径。简单描述,即pair可以将2个数据组合为一个数据,当然pair的返回值也可以是2个数据。

template<class  T1,    class  T2 > struct pair;
其中,T1是first_type,T2是second_type。

有关pair的描述,具体见下:

描述成员1(T1)成员2(T2)
成员类型first_typesecond_type
成员名firstsecond

2.pair使用

2.1成员函数(构造函数、赋值函数)

构造函数描述
pair();默认构造函数。值初始化 pair 的两个元素 first 和 second。
pair( const T1& x, const T2& y );②以 x 初始化 first 并以 y 初始化 second。
template< class U1, class U2 >pair( U1&& x, U2&& y );以 std::forward(x) 初始化 first 并以 std::forward(y) 初始化 second。
template< class U1, class U2 >constexpr pair( pair<U1, U2>& p );③以 p.first 初始化 first 并以 p.second 初始化 second。
template< class U1, class U2 >pair( const pair<U1, U2>& p );以 p.first 初始化 first 并以 p.second 初始化 second。
template< class U1, class U2 >pair( pair<U1, U2>&& p );以std::forward(p.first) 初始化 first 并以 std::forward(p.second) 初始化 second。
template< class U1, class U2 >constexpr pair( const pair<U1, U2>&& p );以 std::forward(p.first) 初始化 first 并以 std::forward(p.second) 初始化 second。
pair& operator=( const pair& other );④复制赋值运算符。以 other 内容的副本替换内容。
template< class U1, class U2 >pair& operator=( const pair<U1, U2>& other );⑤赋值 other.first 给 first , other.second 给 second 。
pair& operator=( pair&& other );⑥移动赋值运算符。用移动语义以 other 的内容替换内容。
template< class U1, class U2 >pair& operator=( pair<U1, U2>&& other );⑦赋值 std::forward(p.first) 给 first , std::forward(p.second) 给 second 。
|
#include <iostream>
#include <utility>
#include <vector>

using namespace std;

int main()
{
    pair<int, string> p1;//①默认构造函数
    cout << "p1.first: " << p1.first << "   p1.second : " << p1.second << endl;

    pair<int, int> p2{ 1,3 };//②以1初始化first并以3初始化 second
    cout << "p2.first: " << p2.first << "   p2.second : " << p2.second << endl;

    pair<int, int> p3{ p2 };//③以p.first初始化first并以p.second初始化second。
    cout << "p3.first: " << p3.first << "   p3.second : " << p3.second << endl;

    pair<int, string> p4{ 3,"Hello World" };
    p1 = p4;//④复制赋值运算符。以 other 内容的副本替换内容。
    cout << "p1.first: " << p1.first << "   p1.second : " << p1.second << endl;

    pair<int, vector<int>> p5{ 1,{1,3,5} }, p6{ 2,{2,4,6} };
    p5 = move(p6);//⑦operator=( pair<U1,U2>&& other );
    cout << "p5.first: " << p5.first  << endl;
    cout << "p6.first: " << p6.first << endl;

    return 0;
}

在这里插入图片描述

2.2非成员函数

非成员函数描述
template< class T1, class T2 >std::pair<T1, T2> make_pair( T1 t, T2 u );构造 std::pair 对象,从参数类型推导目标类型。
template< class T1, class T2 >bool operator==( const std::pair<T1,T2>& lhs, const std::pair<T1,T2>& rhs );若 lhs.first = rhs.first 且 lhs.second = rhs.second 则为 true ,否则为 false
template< class T1, class T2 >bool operator!=( const std::pair<T1,T2>& lhs, const std::pair<T1,T2>& rhs );!(lhs == rhs)
template< class T1, class T2 >bool operator<( const std::pair<T1,T2>& lhs, const std::pair<T1,T2>& rhs );按字典序比较 lhs 和 rhs ,即比较首元素,然后仅若它们等价,再比较第二元素。若 lhs.first<rhs.first 则返回 true 。否则,若 lhs.first>rhs.first 则返回 false 。否则,若 lhs.second<rhs.second 则返回 true 。否则返回 false 。
template< class T1, class T2 >bool operator>( const std::pair<T1,T2>& lhs, const std::pair<T1,T2>& rhs );rhs < lhs
template< class T1, class T2 >bool operator>=( const std::pair<T1,T2>& lhs, const std::pair<T1,T2>& rhs );!(lhs < rhs)
template< class T1, class T2 >void swap( std::pair<T1,T2>& x, std::pair<T1,T2>& y )交换 x 与 y 的内容。等价于 x.swap(y) 。
template <class T, class U>constexpr T& get(std::pair<T, U>& p) ;返回到 p.first 的引用。
template <class T, class U>constexpr const T&& get(const std::pair<U, T>&& p) noexcept;返回到 p.second 的引用。
template< std::size_t I, class T1, class T2 >constexpr std::tuple_element_t<I, std::pair<T1,T2> >& get( std::pair<T1, T2>& p )若 I0 则返回到 p.first 的引用,若 I1 则返回到 p.second 的引用。
#include <iostream>
#include <utility>

using namespace std;

int main()
{
    pair<int, string> p1;//①默认构造函数

    p1 = make_pair( 1,"Hello" );
    cout << "p1.first: " << p1.first << "   p1.second : " << p1.second << endl;

    return 0;
}

在这里插入图片描述

#include <iostream>
#include <utility>
#include <vector>

using namespace std;

int main()
{
    pair<int, int> p1{ 1,3 };
    pair<int, int> p2{ 1,3 };
    pair<int, int> p3{ 2,3 };
    pair<int, int> p4{ 0,3 };
    pair<int, int> p5{ 0,0 };
    pair<int, int> p6{ 2,3 };
    cout << (p1 == p2) << endl;
    cout << (p1 < p3) << endl;
    cout << (p1 > p2) << endl;
    cout << (p1 != p2) << endl;
    cout << (p1 <= p2) << endl; 

    return 0;
}

在这里插入图片描述

#include <iostream>
#include <utility>
#include <vector>

using namespace std;

int main()
{
    pair<int, int> p1{ 1,3 };
    
    int a = get<0>(p1);
    cout << a << endl;

    return 0;
}

在这里插入图片描述

2.3辅助类

辅助类描述
template <class T1, class T2>struct tuple_size<std::pair<T1, T2>> : std::integral_constant<std::size_t, 2> { };std::tuple_size 对 pair 的部分特化提供使用类 tuple 语法,在编译时获得 pair 中元素数的方法,该数总是 2 。
template< std::size_t I, class T1, class T2 >
struct tuple_element<I, std::pair<T1, T2>>;std::tuple_element 对 pair 的部分特化使用类 tuple 语法,提供 pair 元素类型的编译时访问。

使用

输出pair中的所有元素

#include <iostream>
#include <utility>
#include <vector>

using namespace std;

int main()
{
    pair<int, int> p1[]{ {1,3},{2,5},{3,7} };
    
    for (const auto& [value, num] : p1) 
    {
        cout << value << " " << num << endl;
    }
    return 0;
}

在这里插入图片描述

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/9654.html

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

相关文章

Kotlin MVVM之Jetpack系列ViewModel、LiveData的简单使用

一、MVVM是什么&#xff1f; MVVM分为Model&#xff0c;View&#xff0c;ViewModel 三个部分 Model:数据层&#xff0c;包含数据实体和对数据实体的操作 View:UI层&#xff0c;对应于Activity&#xff0c;XML&#xff0c;负责数据显示以及用户交互。 ViewModel&#xff1a;…

22-Redux-1

//npm init //npm install redux //1 导入redux&#xff08;不能通过es6的方式&#xff09; // commonjs一种 -> node.jsconst redux require(redux)const initialState {counter: 0 } // reducer function reducer(state initialState, action) {switch(action.type) {c…

5 步!用阿里云 Serverless 搭建高质量的图片压缩工具

作者&#xff1a;Regan Yue 本文选自“Serverless 函数计算征集令”活动 什么是 Serverless Serverless 是一种基于云计算的开发方法&#xff0c;它让开发人员可以专注于编写代码来解决业务问题&#xff0c;而不是处理服务器问题。它是独一无二的&#xff0c;因为它支持 Auto …

Rust中级教程:指针生态(引用、原始指针、智能指针)and内存操作(Stack、Heap)

指针的一些概览知识点 1.内存地址&#xff1a;指代内存中单个字节的一个整数。 指针&#xff08;原始指针&#xff09;&#xff1a;就是指向某种类型的一个内存地址。 引用&#xff1a;就是指针&#xff0c;是rust提供的一种抽象&#xff0c;如果是动态大小&#xff0c;就是一…

标记肽MGP-7-氨基-4-甲基香豆素、1926163-53-2、Met-Gly-Pro-AMC

蛋氨酸氨基肽酶1D和2的荧光底物。编号: 152397 中文名称: 标记肽MGP-7-氨基-4-甲基香豆素 英文名: H-Met-Gly-Pro-AMC CAS号: 1926163-53-2 单字母: H2N-MGP-AMC 三字母: H2N-Met-Gly-Pro-AMC 氨基酸个数: 3 分子式: C22H28N4O5S1 平均分子量: 460.55 精确分子量: 460.18 等电点…

【SVN】SVN服务端地址变动,idea切换SVN地址

公司切换了SVN服务端的制度&#xff0c;需要本地对应切换SVN地址&#xff0c;以下为具体步骤 错误方式 直接 项目上右键 --> Subversion --> Relocate &#xff0c;修改 To URL 的值&#xff0c;会报错 https://XXXXX is not the root of the repository 的错误 正确的…

Communication-Efficient Learning of Deep Networks from Decentralized Data

international conference on artificial intelligence and statistics Summary 当前机器学习模型训练中存在着数据隐私保护问题&#xff0c;所以作者提出了FL概念。通过分布式隐私保护进行训练模型。对不平衡、non-IID的数据也更合适。 主要提出了FedSGD和FedAvg算法。FedAv…

Andriod开发R文件爆红相关解决方法及排查方案

1.首先尝试下基本的处理方法&#xff1a; 在IDE中工具栏处选择build 尝试clean project&#xff0c;然后再进行rebuild project或者是make project 若使用的是IDEA或Android studio&#xff0c;在上述方法尝试后可以尝试工具栏中File->Setting->Invalidate Caches/Rest…

微信小程序(基础语法)

文章目录基本组件视图容器viewscroll-viewswiper和swiper-item基础内容组件textrich-text其他常用组件buttonimagenavigator基本模板数据绑定插值表达式事件绑定常用事件在事件处理函数中为data的数据赋值事件传参bindinput的使用小程序中的v-model条件渲染wx:ifhidden列表渲染…

一些关于通信拓扑、图论的内容笔记

链接&#xff1a;https://zhuanlan.zhihu.com/p/373368383 目前主要是这一个系列&#xff0c;记下来怕我过后忘了。 别处看到的&#xff08;大概率3B1B&#xff09;----走桥的问题----d偶数 多智能体一致性问题&#xff08;分蛋糕&#xff09; 入度 出度 &#xff1a;信息流…

007_补充_ Pytorch 反向传播和Neural ODE的反向传播

一、Pytorch反向传播 首先是第一个小例子&#xff0c;训练模型拟合 y true_w * x true_b&#xff0c;模型的参数为 param_w, param_b import torchtrue_w torch.Tensor([[2.0, 3.0], [4.0, 5.0]]) # 初始化真实的参数 true_b torch.Tensor([[1.0, 2.0], [3.0, 4.0]]) #…

linux安装jdk17

登录linux 我使用的是Alibaba Cloud Linux 3.2104 LTS 64位操作系统&#xff0c;登录后结果如下&#xff1a; Welcome to Alibaba Cloud Elastic Compute Service !Updates Information Summary: available7 Security notice(s)5 Important Security notice(s)2 Moderate Sec…

2023最新SSM计算机毕业设计选题大全(附源码+LW)之java校园二手商品交易系统p11v7

毕业设计说实话没有想象当中的那么难&#xff0c;导师也不会说刻意就让你毕设不通过&#xff0c;不让你毕业啥的&#xff0c;你只要不是太过于离谱的&#xff0c;都能通过的。首先你得要对你在大学期间所学到的哪方面比较熟悉&#xff0c;语言比如JAVA、PHP等这些&#xff0c;数…

laravel对于数据量特别大的导出excel的提速方案

背景&#xff1a;一些业务场景需要导出excel的需求&#xff0c;但是面对日益增长的数据&#xff0c;要导出的数据会越来越大。生成表格的时间也会越来越慢。针对这个问题&#xff0c;目前的业务通过两个角度去提速。 1&#xff1a;异步导出 如果数据量达到一定的体量&#xf…

【毕业设计】大数据大众点评评论文本分析 - python 数据挖掘

文章目录0 前言1 爬虫1.1 整体思路1.2 网页爬取和解析1.3 数据存储1.4 反爬虫对抗2 探索性分析与文本数据预处理2.1 探索性分析2.2 数据预处理2.3 词云展示3 文本的情感分析3.1 先上结果3.2 文本特征提取&#xff08;TF-IDF&#xff09;3.3 机器学习建模3.4 最后输出的准确率4 …

java ssh校园拼餐系统

首先在系统前台&#xff0c;游客用户可以经过账号注册&#xff0c;管理员审核通过后&#xff0c;用账号密码登录系统前台&#xff0c;查看拼餐服务、网站公告、文明拼餐员、会员风彩、系统简介、咨询信息、拼餐信息等栏目信息&#xff0c;进行在线咨询和管理员交流&#xff0c;…

LTSPICE使用教程:二极管钳位电路仿真

在我们查看芯片内部的设计电路时&#xff0c;通常会发现以下的电路结构&#xff1a; 当定义pin脚输入电压Vpin&#xff0c; 1.Vpin>VDD,二极管D1导通&#xff0c;D2截止&#xff0c;此时无论怎样继续加大VPIN的输入电压时&#xff0c; 进入到管脚内部的电压会被钳制在Vint…

【RocketMQ中生产者生产消息的高可用机制、消费者消费消息的高可用机制、消息的重试机制、死信队列于死信消息】

一.知识回顾 【0.RocketMQ专栏的内容在这里哟&#xff0c;帮你整理好了&#xff0c;更多内容持续更新中】 【1.Docker安装部署RocketMQ消息中间件详细教程】 【2.RocketMQ生产者发送消息的三种方式:发送同步消息、异步消息、单向消息&案例实战&详细学习流程】 【3.Rock…

野火FPGA入门(5)

文章目录第17讲&#xff1a;触摸按键控制LED灯第18讲&#xff1a;流水灯第19讲&#xff1a;呼吸灯第20讲&#xff1a;状态机第21讲&#xff1a;无源蜂鸣器驱动实验第17讲&#xff1a;触摸按键控制LED灯 触摸按键可分为四大类&#xff1a;电阻式、电容式、红外感应式、表面声波…

调优工具常用命令

语法格式 mysqldumpslow [ OPTS... ] [ LOGS... ] //命令行格式常用到的格式组合 -s 表示按照何种方式排序c 访问次数l 锁定时间r 返回记录t 查询时间al 平均锁定时间ar 平均返回记录数at 平均查询时间 -t 返回前面多少条数据 -g 后边搭配一个正则匹配模式&#xff0c;大小写…