pair类型
pair 我们也叫做 对,是两个数据类型的复合类型。可以用结构体替代,然而,当只有两个数据类型时使用 pair 也比较方便。
- 第一关键字
first
- 第二关键字
second
pair 定义和赋值:
1.通过关键字分别赋值:
pair<int, double> p;
p.first = 10;
p.second = 1.23;
cout << p.first << " " << p.second << endl;
2.通过make_pair 赋值:
pair<int, double> p = make_pair(20, 2.34);
3.通过 {}
赋值(个人比较常用):
pair<int, double> p = {30, 3.45};
pair排序:排序时默认对 first 升序,当 first 相同时对 second 升序。
#include <iostream>
#include <algorithm>
#define x first
#define y second
using namespace std;
typedef pair<int, int> PII;
int n;
PII q[110];
int main() {
int n;
cin >> n;
for(int i = 1; i <= n; i++) cin >> q[i].x >> q[i].y;
sort(q + 1, q + n + 1);
for(int i = 1; i <= n; i++) cout << q[i].x << " " << q[i].y << endl;
return 0;
}
输入:
6
3 2
3 1
3 5
1 8
5 6
7 2
输出:
1 8
3 1
3 2
3 5
5 6
7 2
什么是匿名函数?
C++ 中的匿名函数,类似于 python 的 lambda 函数,也就是在句中定义和声明的一个临时函数,仅在调用时才会创建函数对象,无需在头文件中声明。
[capture](parameters)->return-type{body}
[捕获列表](参数列表)->返回类型-{函数主体}
capture:捕获列表
[]
:捕获列表为空。在函数内无法使用外部变量。[a]
:捕获列表为按值传递形式。在函数内仅能使用传递的变量值,无法改变变量。值在匿名函数生成时便已经确定,后续修改不会影响函数内的变量[&a]
:按引用传递。可改变变量。[=]
:用到的任何外部变量都隐式按值捕获。[&]
:用到的任何外部变量都隐式按引用捕获。return-type
:返回类型。
#include <iostream>
using namespace std;
int main() {
// auto f = [](int a, int b)->int{return a + b;};
// auto f = [](int a, int b){return a + b;};
int x = -1;
auto f = [&](int a, int b){
x = a + b;
// cout << x << endl;
return a + b;
};
cout << f(3, 4) << endl;
cout << x << endl;
return 0;
}
pair 自定义排序:
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
typedef pair<int, int> PII;
vector<PII> q;
int main() {
int n;
cin >> n;
for(int i = 1; i <= n; i++){
int x, y;
cin >> x >> y;
q.push_back({x, y});
}
sort(q.begin(), q.end(), [&](const PII& a, const PII& b){
if(a.first == b.first) return a.second < b.second;
return a.first > b.first;
});
for(auto x : q)
cout << x.first << " " << x.second << endl;
return 0;
}
/*
8
3 1
1 2
2 9
5 4
4 5
2 5
7 7
2 3
*/
unique【补充了解】
该函数的作用是“去除”容器或者数组中相邻元素的重复出现的元素。
这里的去除并非真正意义的 erase,而是把不重复的元素移到前面来,返回值是去重之后的尾地址。
unique 针对的是相邻元素,所以对于顺序错乱的数组成员,或者容器成员,需要先进行排序。
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <vector>
using namespace std;
int main() {
vector<int> v({1, 1, 2, 2, 3, 4, 5});
v.erase(unique(v.begin(), v.end()), v.end());
/*
int p = unique(v.begin(), v.end()) - v.begin();
cout << p << endl;
*/
/*
for(int i = 0; i < p; i++)
cout << v[i] << " ";
*/
for(auto x : v)
cout << x << " ";
return 0;
}
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <ctime>
using namespace std;
int main() {
int a[] = {1, 1, 2, 2, 3, 4, 5};
int p = unique(a, a + 7) - a;
for(int i = 0; i < p; i++)
cout << a[i] << " ";
return 0;
}
random_shuffle【补充了解】
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <vector>
#include <ctime>
using namespace std;
int main() {
srand(time(0));
vector<int> v({1, 2, 3, 4, 5, 6});
random_shuffle(v.begin(), v.end());
for(int x : v)
cout << x << " ";
puts("");
sort(v.begin(), v.end(), greater<int>());
for(int x : v)
cout << x << " ";
puts("");
sort(v.begin(), v.end(), less<int>());
for(int x : v)
cout << x << " ";
puts("");
return 0;
}