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_type second_type
成员名 first second

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初始化 secondcout << "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;
}

C++——pair用法总结相关推荐

  1. boost::fusion::pair用法的测试程序

    boost::fusion::pair用法的测试程序 实现功能 C++实现代码 实现功能 boost::fusion::pair用法的测试程序 C++实现代码 #include <boost/c ...

  2. C 的Pair用法分类整理(精)

    1 pair的应用 pair是将2个数据组合成一个数据,当需要这样的需求时就可以使用pair,如stl中的map就是将key和value放在一起来保存.另一个应用是,当一个函数需要返回2个数据的时候, ...

  3. pair用法 lower_bound upper_bound

    int a,b;pair<int,int>p;cin>>a>>b;p=make_pair(a,b);cout<<p.first<<" ...

  4. std::tuple、std::tie(可用于结构体大小比较)、std::pair用法

    1.tuple应用: 解释:是一个元组,可包含无限多不同类型变量,pair的升级版,但没有pair得成员变量first.second. 1.1.代码: // tuple example #includ ...

  5. C++ map()和pair()用法

    #include <map> #include <iostream> using namespace std; int main( ) {map<int, int> ...

  6. pair用法(给元素赋值)

    1,初始化方式 pair<int, int> p(1, 2); 2,单独赋值 pair<int, int> p; p.first = 1; p.second = 2; 3,构造 ...

  7. c++ pair 用法详解

    C++pair类型 标准库类型--pair类型定义在utility头文件中定义 1.pair的创建和初始化 pair包含两个数值,与容器一样,pair也是一种模板类型.但是又与之前介绍的容器不同,在创 ...

  8. C++中的pair用法

    目录 1:pair的定义 2:pair的操作以及使用 3:总结 -------------------------------------------------------------------- ...

  9. C++ pair用法

    pair相当于是包含有两个变量的struct,同样类型的pair变量可以直接赋值,这里比struct要方便,写的时候也很简单,确实很好用,而且first和second可以直接调出里面的两个元素. 1. ...

最新文章

  1. -Objc 、 -all_load 、 -force_load
  2. 使用ilmerge实现.net程序静态链接
  3. 通过编程计算一个游戏的胜率
  4. IntelliJ IDEA 常用快捷键和设置
  5. JAVAWEB入门之Servlet相关配置
  6. Spring Boot数据持久化之NamedParameterJdbcTemplate
  7. 格拉布斯(Grubbs)准则法
  8. 【Python】torrentParser1.01
  9. html简易登陆注册模板
  10. 国外调查问卷怎么做?
  11. oracle创建交叉表,SQL交叉表常用实例(转载网络)
  12. β冲刺第二周第二次例会报告
  13. express+socket.io 共享session
  14. graphpad画生存曲线怎么样去掉删失点_手把手教你用GraphPad Prism绘制生存曲线
  15. Python绘制动态图形
  16. 嵌入式C语言强化学习——(嵌入式学习路线1)
  17. Ubuntu系统的有线网卡驱动问题
  18. Unity3D插件之EasyTouch5入门
  19. 伦敦大学学院的新研究报告揭示主流分布式账本技术网络的能耗水平
  20. python | requests库基本应用

热门文章

  1. 六、JConsole性能分析
  2. Android - 原生登录注册页面【仿】淘宝App
  3. sigbus内存错误
  4. 仓鼠的鸡蛋(线段树)
  5. 高级信息系统项目管理师—论文—进度管理
  6. 怎么查oracle ocm证书,Oracle OCM认证
  7. i9-13900H参数 酷睿i913900H性能怎么样 相当于什么水平
  8. 五年制转本学历很重要江苏专转本
  9. 对比两个文件不一样的软件
  10. 编程突然光标变粗变黑解决办法