博客
关于我
pair的用法
阅读量:552 次
发布时间:2019-03-09

本文共 1375 字,大约阅读时间需要 4 分钟。

#include <bits/stdc++.h>

using namespace std;

// 比较结构cmp1,按first降序,否则按second升序

struct cmp1{
bool operator()(const pair<int,int>& a, const pair<int,int>& b){
if(a.first != b.first)
return a.first > b.first;
else
return a.second < b.second;
}

// 比较结构cmp2,按second升序,否则按first降序

struct cmp2{
bool operator()(const pair<int,int>& q1, const pair<int,int>& q2){
if(q1.first == q2.first)
return q1.second > q2.second;
else
return q1.first < q2.first;
}

int main(){

// pair的基本用法
pair<int,double> p1(1, 2.2);
pair<int,double> p2;
p2 = p1;
cout << p2.first << " " << p2.second << endl;
p2 = make_pair(1,3);
cout << p2.first << " " << p2.second << endl;

// pair结合其他的用法  pair
p3[10]; for(int i = 0; i < 5; ++i){ p3[i] = make_pair(i, i+1); } sort(p3, p3+5, cmp1); for(int i = 0; i < 5; ++i){ cout << p3[i].first << " " << p3[i].second << endl; } vector
> p4; for(int i = 0; i < 5; ++i){ p4.push_back({1, i+1}); p4.push_back(make_pair(i, i+1)); } sort(p4.begin(), p4.end(), cmp1); int len = p4.size(); for(int i = 0; i < len; ++i){ cout << p4[i].first << " " << p4[i].second << endl; } priority_queue
, vector
>, Greater
> p5; p5.push({2,2}); p5.push({1,2}); p5.push({2,3}); while(!p5.empty()){ cout << p5.top().first << " " << p5.top().second << endl; p5.top(); p5.pop(); }

}

转载地址:http://onwpz.baihongyu.com/

你可能感兴趣的文章
mysql 字段合并问题(group_concat)
查看>>
mysql 字段类型类型
查看>>
MySQL 字符串截取函数,字段截取,字符串截取
查看>>
MySQL 存储引擎
查看>>
mysql 存储过程 注入_mysql 视图 事务 存储过程 SQL注入
查看>>
MySQL 存储过程参数:in、out、inout
查看>>
mysql 存储过程每隔一段时间执行一次
查看>>
mysql 存在update不存在insert
查看>>
Mysql 学习总结(86)—— Mysql 的 JSON 数据类型正确使用姿势
查看>>
Mysql 学习总结(87)—— Mysql 执行计划(Explain)再总结
查看>>
Mysql 学习总结(88)—— Mysql 官方为什么不推荐用雪花 id 和 uuid 做 MySQL 主键
查看>>
Mysql 学习总结(89)—— Mysql 库表容量统计
查看>>
mysql 实现主从复制/主从同步
查看>>
mysql 审核_审核MySQL数据库上的登录
查看>>
mysql 导入 sql 文件时 ERROR 1046 (3D000) no database selected 错误的解决
查看>>
mysql 导入导出大文件
查看>>
MySQL 导出数据
查看>>
mysql 将null转代为0
查看>>
mysql 常用
查看>>
MySQL 常用列类型
查看>>