C++ STL pair 类模板(深入了解,一文学会)

        关联式容器存储的是“键值对”形式的数据,比如:map<int,string>  int对应相应的string字符串 key和value对一对一对应关系。

注意,基于各个关联式容器存储数据的特点,只有各个键值对中的键和值全部对应相等时,才能使用 set 和 multiset 关联式容器存储,否则就要选用 map 或者 multimap 关联式容器。

这个怎么理解呢?

        各个键值对中的键和值全部对应相等时,才能使用 set 和 multiset 关联式容器存储 例如:

<a,a>
<b,b>
<c,c>
<1,1>
<2,2>
<3,3>

这样才算 键和值全部对应相等 ,详细的 set 和 multiset 容器请看后续文章。

        map 或者 multimap 例如:

<a,1>
<b,2>
<c,3>
<1,a>
<2,b>
<3,c>

需要注意的是如果使用 map 或者 multimap 的话KEY值必须唯一。

  本文作者原创,转载请附上文章出处与本文链接。

C++ STL pair 类模板(深入了解,一文学会)目录

1. pair 模板函数

2. pair 模板使用

2.1 pair 模板初始化

2.2 pair 模板手动初始化

2.3 pair模板比较

2.4 swap() 成员函数


        C++ STL 标准库提供了 pair 类模板,其专门用来将 2 个普通元素 first 和 second(可以是 C++ 基本数据类型、结构体、类自定的类型)创建成一个新元素<first, second>。通过其构成的元素格式不难看出,使用 pair 类模板来创建“键值对”形式的元素,再合适不过。

1. pair 模板函数

默认构造函数,即创建空的 pair 对象pair();
直接使用 2 个元素初始化成 pair 对象pair (const first_type& a, const second_type& b);
拷贝(复制)构造函数,即借助另一个 pair 对象,创建新的 pair 对象template<class U, class V> pair (const pair<U,V>& pr);
移动构造函数 template<class U, class V> pair (pair<U,V>&& pr);
使用右值引用参数,创建 pair 对象template<class U, class V> pair (U&& a, V&& b);

 

2. pair 模板使用

2.1 pair 模板初始化

        程序在创建 pair4 对象时,调用了 make_pair() 函数,它也是 <utility> 头文件提供的,其功能是生成一个 pair 对象。因此,当我们将 make_pair() 函数的返回值(是一个临时对象)作为参数传递给 pair() 构造函数时,其调用的是移动构造函数,而不是拷贝构造函数。

///创建 pair 对象的方法:
	// 调用构造函数 1,也就是默认构造函数
	pair <string, double> pair1;
	// 调用第 2 种构造函数
	pair <string, string> pair2("a", "csdn");
	// 调用拷贝构造函数
	pair <string, string> pair3(pair2);
	//调用移动构造函数
	pair <string, string> pair4(make_pair("b", "dreambegins"));
	// 调用第 5 种构造函数
	pair <string, string> pair5(string("c"), string("vip"));
	cout << "pair1: " << pair1.first << " " << pair1.second << endl;
	cout << "pair2: " << pair2.first << " " << pair2.second << endl;
	cout << "pair3: " << pair3.first << " " << pair3.second << endl;
	cout << "pair4: " << pair4.first << " " << pair4.second << endl;
	cout << "pair5: " << pair5.first << " " << pair5.second << endl;
	cout << "-------------------------------------------------------------------" << endl;
	cout << "-------------------------------------------------------------------" << endl;

2.2 pair 模板手动初始化

          <utility>头文件中除了提供创建 pair 对象的方法之外,还为 pair 对象重载了 <、<=、>、>=、==、!= 这 6 的运算符,其运算规则是:对于进行比较的 2 个 pair 对象,先比较 pair.first 元素的大小,如果相等则继续比较 pair.second 元素的大小。

///C++ 11 还允许我们手动为 pair 对象赋值,比如:
	pair <string, double> pair6;
	pair6.first = "C++";
	pair6.second = 1.1;
	cout << "new pair6: " << pair6.first << " " << pair6.second << endl;
	cout << "-------------------------------------------------------------------" << endl;
	cout << "-------------------------------------------------------------------" << endl;

///上面程序中 pair 对象的创建过程,还可以写入如下形式,它们是完全等价的: 
	pair <string, string> pair7 = make_pair("C++教程", "csdn");
	cout << "pair7: " << pair7.first << " " << pair7.second << endl;
	cout << "-------------------------------------------------------------------" << endl;
	cout << "-------------------------------------------------------------------" << endl;

2.3 pair模板比较

        对于进行比较的 2 个 pair 对象,其对应的键和值的类型比较相同,否则将没有可比性,同时编译器提示没有相匹配的运算符,即找不到合适的重载运算符。

	pair <string, int> pair8("STL教程", 20);
	pair <string, int> pair9("C++教程", 20);
	pair <string, int> pair10("C++教程", 30);

	//pair9和pair8的key不同,value相同
	if (pair8 != pair9) {
		cout << "pair8 != pair9" << endl;
	}
	//pair9和pair3的pair10相同,value不同
	if (pair9 != pair10) {
		cout << "pair9 != pair10" << endl;
	}
	cout << "-------------------------------------------------------------------" << endl;
	cout << "-------------------------------------------------------------------" << endl;

2.4 swap() 成员函数

        pair类模板还提供有一个 swap() 成员函数,能够互换 2 个 pair 对象的键值对,其操作成功的前提是这 2 个 pair 对象的键和值的类型要相同。例如:

	pair <string, int> pair11("pair11", 10);
	pair <string, int> pair12("pair12", 20);
	//交换 pair1 和 pair2 的键值对
	pair11.swap(pair12);
	cout << "pair11: " << pair11.first << " " << pair11.second << endl;
	cout << "pair12: " << pair12.first << " " << pair12.second << endl;
	cout << "-------------------------------------------------------------------" << endl;
	cout << "-------------------------------------------------------------------" << endl;

以下博客部分内容借鉴自:http://c.biancheng.net/stl/。

C++ STL 容器、迭代器、适配器(深入了解,一文学会)    https://blog.csdn.net/qq_37529913/article/details/120052137                                                                                C++ STL deque容器(深入了解,一文学会)                       https://blog.csdn.net/qq_37529913/article/details/118676574
C++ STL vector容器(深入了解,一文学会)                       https://blog.csdn.net/qq_37529913/article/details/118676109
C++ STL list容器(深入了解,一文学会)                             https://blog.csdn.net/qq_37529913/article/details/118676917
C++ STL forward_list容器(深入了解,一文学会)               https://blog.csdn.net/qq_37529913/article/details/118687348
C++ STL array 容器(深入了解,一文学会)                        https://blog.csdn.net/qq_37529913/article/details/118688364
C++ STL pair 类模板(深入了解,一文学会)                       https://blog.csdn.net/qq_37529913/article/details/118714852
C++ STL map容器(深入了解,一文学会)                           https://blog.csdn.net/qq_37529913/article/details/118741670
C++ STL map emplace()和emplace_hint()(深入了解,一文学会)         https://blog.csdn.net/qq_37529913/article/details/118771777
C++ STL multimap容器(深入了解,一文学会)                    https://blog.csdn.net/qq_37529913/article/details/118773021
C++ STL Set容器(深入了解,一文学会)                             https://blog.csdn.net/qq_37529913/article/details/118918940
C++ STL multiset容器(深入了解,一文学会)                      https://blog.csdn.net/qq_37529913/article/details/119624779
C++ STL unordered_map容器(深入了解,一文学会)         https://blog.csdn.net/qq_37529913/article/details/119689199
C++ STL unordered_set容器(深入了解,一文学会)           https://blog.csdn.net/qq_37529913/article/details/119709019
C++ STL unordered_multiset容器(深入了解,一文学会)    https://blog.csdn.net/qq_37529913/article/details/119709079
C++ STL stack容器适配器(深入了解,一文学会)        https://blog.csdn.net/qq_37529913/article/details/119723782
C++ STL queue容器适配器(深入了解,一文学会)       https://blog.csdn.net/qq_37529913/article/details/119746246
C++ STL priority_queue容器适配器(深入了解,一文学会)                https://blog.csdn.net/qq_37529913/article/details/119770527
C++ STL reverse_iterator反向迭代器适配器(深入了解,一文学会)   https://blog.csdn.net/qq_37529913/article/details/119814820
C++ STL insert_iterator插入迭代器适配器(深入了解,一文学会)      https://blog.csdn.net/qq_37529913/article/details/119834378
C++ STL stream_iterator流迭代器(深入了解,一文学会)                  https://blog.csdn.net/qq_37529913/article/details/119834429
C++ STL streambuf_iterator流缓冲区迭代器(深入了解,一文学会)        https://blog.csdn.net/qq_37529913/article/details/119850048
C++ STL move_iterator移动迭代器(深入了解,一文学会)                      https://blog.csdn.net/qq_37529913/article/details/119859888
C++ STL advance()函数(深入了解,一文学会)        https://blog.csdn.net/qq_37529913/article/details/120008250
C++ STL distance()函数(深入了解,一文学会)        https://blog.csdn.net/qq_37529913/article/details/120008300
C++ STL iterator迭代器(深入了解,一文学会)         https://blog.csdn.net/qq_37529913/article/details/120008346
C++ STL const_iterator转换为iterator类型迭代器(深入了解,一文学会)        https://blog.csdn.net/qq_37529913/article/details/120008324
C++ STL begin()和end()函数(深入了解,一文学会)        https://blog.csdn.net/qq_37529913/article/details/120008459
C++ STL prev()和next()函数(深入了解,一文学会)         https://blog.csdn.net/qq_37529913/article/details/120008481
 

  • 1
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

双子座断点

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值