pair(组)干货归纳+用法示例

一.pair特点

  1. 当想要把两个元素绑在一起作为一个合成元素、又不想要因此定义结构体时,使用pair可以很方便地作为一个代替品。
  2. pair是单个数据对的操作,pair是一struct类型,有两个成员变量,通过first,second来访问,用的是“.”访问。
  3. map是一个关联容器,里面存放的是键值对,容器中每一元素都是pair类型,通过map的insert()方法来插入元素(pair类型)。

二.pair变量的创建、使用方法

1.pair头文件

#include<utility>
//也可以直接用#include <map>
using namespace std;

2.pair元素的创建

   pair<int, double> p1;  //使用默认构造函数
   pair<int, double> p2(1, 2.4);  //用给定值初始化
   pair<int, double> p3(p2);  //拷贝构造函数
   auto myPair = std::make_pair(10, 3.14);

3.访问pair内部元素(通过first和second)

 pair<int, double> p1;  //使用默认构造函数
 p1.first = 1;
 p1.second = 2.5;
 cout << p1.first << ' ' << p1.second << endl;

在这里插入图片描述

4.给pair变量赋值

#include "pch.h"
#include<utility>
#include<iostream>
#include<string>
using namespace std;
int main()
{
	pair<string, int> p;
	p.first = "haha";
	p.second = 5;
	cout << p.first << " " << p.second << endl;

	p = make_pair("xixi", 55);
	cout << p.first << " " << p.second << endl;

	p = pair<string, int>("heihei", 555);
	cout << p.first << " " << p.second << endl;

	pair<string, int> p1 = p;
	cout << p.first << " " << p.second << endl;
}

在这里插入图片描述

5.pair常用函数

  • (两个pair类型数据可以直接使用=、!=、<、<=、>、>=比较大小比较规则是先以first的大小作为标准,只有当first相等时采取判别second的大小)
#include "pch.h"
#include<utility>
#include<cstdio>
#include<string>
using namespace std;
int main()
{
	pair<int, int> p1(5, 10);
	pair<int, int> p2(5, 15);
	pair<int, int> p3(10, 5);
	if (p1 < p3)
		printf("p1<p3\n");
	if (p1 <= p3)
		printf("p1<=p3\n");
	if (p1 < p2)
		printf("p1<p2\n");
	return 0;
}

在这里插入图片描述

6.pair的常见用途

(1)用来代替二元结构体及其构造函数,可以节省编码时间;
(2)作为map的键值对来进行插入,示例如下:

#include "pch.h"
#include<utility>
#include<iostream>
#include<cstdio>
#include<string>
#include<map>
using namespace std;
int main()
{
	map<string, int> mp;
	mp.insert(make_pair("heihei", 5));
	mp.insert(pair<string, int>("haha", 10));
	for (map<string, int>::iterator it = mp.begin(); it != mp.end(); it++)
	{
		cout << it->first << " " << it->second << endl;
	}
	return 0;
}

在这里插入图片描述

三.利用algorithm库对pair进行操作

1.std::sort:

可以对包含pair对象的序列进行排序。默认情况下,它将按照pair的第一个元素进行排序,也可以通过提供自定义的比较函数来指定排序规则。例如:

#include <algorithm>
#include <utility>
#include <vector>

bool comparePairs(const std::pair<int, double>& p1, const std::pair<int, double>& p2) {
    return p1.second < p2.second;
}

int main() {
    std::vector<std::pair<int, double>> pairs = {{2, 3.0}, {1, 1.5}, {3, 2.5}};
    std::sort(pairs.begin(), pairs.end(), comparePairs);
    // pairs 现在为 {{1, 1.5}, {3, 2.5}, {2, 3.0}}
    return 0;
}

2.std::for_each:

可以对包含pair对象的序列中的每个元素执行某个函数。例如:

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

void printPair(const std::pair<int, double>& p) {
    std::cout << "(" << p.first << ", " << p.second << ")" << std::endl;
}

int main() {
    std::vector<std::pair<int, double>> pairs = {{1, 1.5}, {2, 3.0}, {3, 2.5}};
    std::for_each(pairs.begin(), pairs.end(), printPair);
    return 0;
}

3.std::find_if:

可以在包含pair对象的序列中查找满足某个条件的元素。例如:

#include <algorithm>
#include <utility>
#include <vector>

bool isSecondGreaterThanTwo(const std::pair<int, double>& p) {
    return p.second > 2.0;
}

int main() {
    std::vector<std::pair<int, double>> pairs = {{1, 1.5}, {2, 3.0}, {3, 2.5}};
    auto it = std::find_if(pairs.begin(), pairs.end(), isSecondGreaterThanTwo);
    if (it != pairs.end()) {
        std::cout << "Found pair with second element > 2.0: (" << it->first << ", " << it->second << ")" << std::endl;
    }
    return 0;
}

4.std::accumulate:

可以对包含pair对象的序列进行累加或其他二元操作。该函数接受三个参数:迭代器范围、累加器的初始值和一个二元操作函数。该函数将返回通过二元操作函数执行累加或其他操作的结果。例如:

#include <algorithm>
#include <utility>
#include <vector>

double sumSecondElements(double acc, const std::pair<int, double>& p) {
    return acc + p.second;
}

int main() {
    std::vector<std::pair<int, double>> pairs = {{1, 1.5}, {2, 3.0}, {3, 2.5}};
    double sum = std::accumulate(pairs.begin(), pairs.end(), 0.0, sumSecondElements);
    std::cout << "Sum of second elements: " << sum << std::endl;
    return 0;
}

5.std::transform`:

可以对包含pair对象的序列中的每个元素执行某个操作,并将结果存储在另一个序列中。例如:

#include <algorithm>
#include <utility>
#include <vector>

double addOneToSecond(const std::pair<int, double>& p) {
    return p.second + 1.0;
}

int main() {
    std::vector<std::pair<int, double>> pairs = {{1, 1.5}, {2, 3.0}, {3, 2.5}};
    std::vector<double> result;
    std::transform(pairs.begin(), pairs.end(), std::back_inserter(result), addOneToSecond);
    // result 现在为 {2.5, 4.0, 3.5}
    return 0;
}

  • 8
    点赞
  • 40
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

LiuZuqiang_3027

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

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

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

打赏作者

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

抵扣说明:

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

余额充值