C++ STL中的 pair

pair 概述

pair 用于将两个可能是不同数据类型的值结合在一起。pair 提供了一种将两个异构对象存储为单个单元的方法。如果想存储元组,基本上会使用它。pair 容器是一个定义在 <utility> 头文件中的简单容器,由两个数据元素或对象组成。

  • 第一个元素通过 first 引用,第二个元素通过 second 引用,顺序是固定的 (first, second)
  • pair 可以赋值、拷贝和比较。map 和 hash_map 中分配的对象数组默认是 'pair' 类型的,其中所有的 'first' 元素都是与其 'second' 值对象关联的唯一键。
  • 为了访问 pair 元素,使用变量名后跟.运算符,后跟关键字 first 或 second。

语法

cpp 复制代码
pair<data_type1, data_type2> Pair_name

例子:

cpp 复制代码
// CPP program to illustrate Pair in STL
#include <iostream>
#include <utility>
using namespace std;

// Driver Code
int main()
{
	// defining a pair
	pair<int, char> PAIR1;

	// first part of the pair
	PAIR1.first = 100;
	// second part of the pair
	PAIR1.second = 'G';

	cout << PAIR1.first << " ";
	cout << PAIR1.second << endl;

	return 0;
}

输出

shell 复制代码
100 G

初始化Pair:也可以初始化pair。

语法

cpp 复制代码
pair<data_type1, data_type2> Pair_name(value1, value2);

初始化 pair 的不同方法:

cpp 复制代码
pair  g1;         //default
pair  g2(1, 'a');  //initialized,  different data type
pair  g3(1, 10);   //initialized,  same data type
pair  g4(g3);    //copy of g3

另一种初始化pair的方法是使用 make_pair() 函数。

cpp 复制代码
g2 = make_pair(1, 'a');

另一种声明 pair 的有效语法是:

cpp 复制代码
g2 = {1, 'a'};

代码示例:

cpp 复制代码
// CPP program to illustrate
// Initializing of pair STL
#include <iostream>
#include <utility>
using namespace std;

// Driver Code
int main()
{
	// defining a pair
	pair<string, double> PAIR2("GeeksForGeeks", 1.23);

	cout << PAIR2.first << " ";
	cout << PAIR2.second << endl;

	return 0;
}

输出:

bash 复制代码
GeeksForGeeks 1.23

注意: 如果没有初始化,会自动初始化 pair 的 first 值。

cpp 复制代码
// CPP program to illustrate 
// auto-initializing of pair STL
#include <iostream>
#include <utility>

using namespace std;

int main()
{
	pair<int, double> PAIR1;
	pair<string, char> PAIR2;

	// it is initialised to 0
	cout << PAIR1.first; 

	// it is initialised to 0
	cout << PAIR1.second; 

	cout << " ";

	// // it prints nothing i.e NULL
	cout << PAIR2.first; 
	
	// it prints nothing i.e NULL
	cout << PAIR2.second; 

	return 0;
}

输出

bash 复制代码
00

成员函数

1)make_pair():这个模板函数允许在不显示写明类型的情况下创建 pair 值对。

语法

cpp 复制代码
Pair_name = make_pair (value1,value2);

例子:

cpp 复制代码
// CPP Program to demonstrate make_pair()
// function in pair
#include <iostream>
#include <utility>
using namespace std;

// Driver Code
int main()
{
	pair<int, char> PAIR1;
	pair<string, double> PAIR2("GeeksForGeeks", 1.23);
	pair<string, double> PAIR3;

	PAIR1.first = 100;
	PAIR1.second = 'G';

	PAIR3 = make_pair("GeeksForGeeks is Best", 4.56);

	cout << PAIR1.first << " ";
	cout << PAIR1.second << endl;

	cout << PAIR2.first << " ";
	cout << PAIR2.second << endl;

	cout << PAIR3.first << " ";
	cout << PAIR3.second << endl;

	return 0;
}

输出

bash 复制代码
100 G
GeeksForGeeks 1.23
GeeksForGeeks is Best 4.56

2)swap:该函数交换两个 pair 对象的内容。两个 pair 必须是相同类型的。

语法

cpp 复制代码
pair1.swap(pair2);

对于给定的两个同类型的 pair1 和 pair2,swap 函数会将 pair1.first 和 pair2.first 交换,pair1.second 和 pair2.second 进行交换。

例子:

cpp 复制代码
// CPP Program to demonstrate swap()
// function in pair
#include <iostream>
#include <utility>

using namespace std;

// Driver Code
int main()
{
	pair<char, int> pair1 = make_pair('A', 1);
	pair<char, int> pair2 = make_pair('B', 2);

	cout << "Before swapping:\n ";
	cout << "Contents of pair1 = " << pair1.first << " "
		<< pair1.second;
	cout << "Contents of pair2 = " << pair2.first << " "
		<< pair2.second;
	pair1.swap(pair2);

	cout << "\nAfter swapping:\n ";
	cout << "Contents of pair1 = " << pair1.first << " "
		<< pair1.second;
	cout << "Contents of pair2 = " << pair2.first << " "
		<< pair2.second;

	return 0;
}

输出

bash 复制代码
Before swapping:
 Contents of pair1 = A 1Contents of pair2 = B 2
After swapping:
 Contents of pair1 = B 2Contents of pair2 = A 1

3)tie():此函数的工作原理与tuples(元组)相同。它创建了一个对其参数的左值引用元组,即将元组(此时是pair)拆包为单独的变量。就像在元组中一样,这里也有 tie 的两种变体,有和没有"ignore"。"ignore" 关键字会忽略特定的元组元素,使其无法拆包。

但是,元组可以有多个参数,但是pair只能有两个参数。因此,在pair的情况下,需要显式处理拆包。

语法

cpp 复制代码
tie(int &, int &) = pair1; 

例子:

cpp 复制代码
// CPP code to illustrate tie() in Pair
#include <bits/stdc++.h>
using namespace std;

// Driver Code
int main()
{
	pair<int, int> pair1 = { 1, 2 };
	int a, b;
	tie(a, b) = pair1;
	cout << a << " " << b << "\n";

	pair<int, int> pair2 = { 3, 4 };
	tie(a, ignore) = pair2;

	// prints old value of b
	cout << a << " " << b << "\n";

	// Illustrating pair of pairs
	pair<int, pair<int, char> > pair3 = { 3, { 4, 'a' } };
	int x, y;
	char z;

	// tie(x,y,z) = pair3; Gives compilation error
	// tie(x, tie(y,z)) = pair3; Gives compilation error
	// Each pair needs to be explicitly handled
	tie(x,ignore) = pair3;
	tie(y, z) = pair3.second;
	cout << x << " " << y << " " << z << "\n";
}

// contributed by sarthak_eddy.

输出

bash 复制代码
1 2
3 2
3 4 a

说明pair中函数的代码:

cpp 复制代码
// CPP program to illustrate pair in STL
#include <iostream>
#include <string>
#include <utility>
using namespace std;

int main()
{
	pair<string, int> g1;
	pair<string, int> g2("Quiz", 3);
	pair<string, int> g3(g2);
	pair<int, int> g4(5, 10);

	g1 = make_pair(string("Geeks"), 1);
	g2.first = ".com";
	g2.second = 2;

	cout << "This is pair g" << g1.second << " with "
		<< "value " << g1.first << "." << endl
		<< endl;

	cout << "This is pair g" << g3.second << " with value "
		<< g3.first
		<< "This pair was initialized as a copy of "
		<< "pair g2" << endl
		<< endl;

	cout << "This is pair g" << g2.second << " with value "
		<< g2.first << "\nThe values of this pair were"
		<< " changed after initialization." << endl
		<< endl;

	cout << "This is pair g4 with values " << g4.first
		<< " and " << g4.second
		<< " made for showing addition. \nThe "
		<< "sum of the values in this pair is "
		<< g4.first + g4.second << "." << endl
		<< endl;

	cout << "We can concatenate the values of"
		<< " the pairs g1, g2 and g3 : "
		<< g1.first + g3.first + g2.first << endl
		<< endl;

	cout << "We can also swap pairs "
		<< "(but type of pairs should be same) : " << endl;
	cout << "Before swapping, "
		<< "g1 has " << g1.first << " and g2 has "
		<< g2.first << endl;
	swap(g1, g2);
	cout << "After swapping, "
		<< "g1 has " << g1.first << " and g2 has "
		<< g2.first;

	return 0;
}

输出

bash 复制代码
This is pair g1 with value Geeks.

This is pair g3 with value QuizThis pair was initialized as a copy of pair g2

This is pair g2 with value .com
The values of this pair were changed after initialization.

This is pair g4 with values 5 and 10 made for showing addition. 
The sum of the values in this pair is 15.

We can concatenate the values of the pairs g1, g2 and g3 : GeeksQuiz.com

We can also swap pairs (but type of pairs should be same) : 
Before swapping, g1 has Geeks and g2 has .com
After swapping, g1 has .com and g2 has Geeks

时间复杂度 : O ( 1 ) O(1) O(1)
空间复杂度 : O ( 1 ) O(1) O(1)

Pair 中的运算符(=, ==, !=, >=, <=)

pair 中也可以使用运算符。

1)使用等于(=):为pair对象赋值一个新对象。语法:

cpp 复制代码
pair& operator= (const pair& pr);

赋值"pr" 作为 "pair" 对象的新内容。first 值被赋值为 pr 的first 值,second值被赋值为 pr 的second 值。

2)pair 的比较运算符(==):给定的 pair1 和 pair2,比较运算符比较两个pair的 first 值和 second 值,即 pair1.first 是否等于 pair2.first 以及 pair2.first 是否等于 pair2.second。

即 是否 ((pari1.first ==pair2.first) && (pair1.second==pair2.second))

两个条件中的任意一个是 false,就返回 false,否则返回 true。

3)pair的不等运算符(!=):给定的 pair1 和 pair2,!= 运算符比较两个pair的first值,即 pair1.first 是否等于 pair2.first,如果相等,检查两个 pair 的 second 值。

4)pair的逻辑运算符(>=, <=):对于给定的两个 pair,比如 pair1 和 pair2,=,>也可以与 pair 一起使用。它通过比较 pair 的第一个值来返回 0 或者 1。对于像 p1=(1,20) 和 p2=(1,10) 这样的对,p2 < p1 应该结果为0(因为它只比较第一个元素是否相等,所以它肯定不小于),但是这是不对的。在这里,pair 比较第二个元素,如果它满足,则返回1(只有当第一个元素在仅使用关系运算符 > 或 < 相等时才会出现这种情况,否则这些运算符的工作方式如上所示)。

相关推荐
星星法术嗲人6 分钟前
【Java】—— 集合框架:Collections工具类的使用
java·开发语言
黑不溜秋的20 分钟前
C++ 语言特性29 - 协程介绍
开发语言·c++
一丝晨光25 分钟前
C++、Ruby和JavaScript
java·开发语言·javascript·c++·python·c·ruby
天上掉下来个程小白27 分钟前
Stream流的中间方法
java·开发语言·windows
xujinwei_gingko38 分钟前
JAVA基础面试题汇总(持续更新)
java·开发语言
￴ㅤ￴￴ㅤ9527超级帅39 分钟前
LeetCode hot100---二叉树专题(C++语言)
c++·算法·leetcode
sp_wxf1 小时前
Lambda表达式
开发语言·python
Fairy_sevenseven1 小时前
【二十八】【QT开发应用】模拟WPS Tab
开发语言·qt·wps
_GR1 小时前
每日OJ题_牛客_牛牛冲钻五_模拟_C++_Java
java·数据结构·c++·算法·动态规划
蜡笔小新星1 小时前
Python Kivy库学习路线
开发语言·网络·经验分享·python·学习