set的基础特性:
- set是一个集合容器。
- 它会将存入的元素自动排序并去重
- 使用迭代器指针寻找遍历
- 【multiset是一个不会去重的set】其用法和set一样。
有关set的主要函数:
set<int>p; //建立一个可以是任何类型的set
p.insert(a); //将a元素插入元素到set
p.erase(a); //删除a元素,可以是数值,也可以是迭代器
p.begin(); //返回第一个元素的迭代器
p.end(); //返回最后一个元素的迭代器{返回的是一个地址位置}
p.clear(); //清空set
p.count(a); //返回a元素的个数
p.find(a); //返回a元素的迭代器
p.size(); //返回集合中元素的个数
p.lower_bound(a); //返回指向大于等于a的第一个元素的迭代器
p.upper_bound(a); //返回指向小于等于a的第一个元素的迭代器
p.equal_range(a); //返回集合中与a元素相等的上下两个元素的迭代器
set的遍历:
set的遍历采用迭代器遍历,先建立一个游标进行遍历
set<int>::iterator it=p.begin();
//建立一个迭代器,使其指向set的begin()位置
for(it;it!=p.end();it++) //执行到set最后截止
{
cout<<*it<<" ";
}
multiset:
直接上例题吧,这个的使用和set基本一样
eg:
You are given two arrays a and b, both of length n. All elements of both arrays are from 0 to n−1.
You can reorder elements of the array b (if you want, you may leave the order of elements as it is). After that, let array c be the array of length n, the i-th element of this array is ci=(ai+bi)%n, where x%y is x modulo y.
Your task is to reorder elements of the array b to obtain the lexicographically minimum possible array c.
Array x of length n is lexicographically less than array y of length n, if there exists such i (1≤i≤n), that xi<yi, and for any j (1≤j<i) xj=yj.
Input
The first line of the input contains one integer n (1≤n≤2⋅105) — the number of elements in a, b and c.
The second line of the input contains n integers a1,a2,…,an (0≤ai<n), where ai is the i-th element of a.
The third line of the input contains n integers b1,b2,…,bn (0≤bi<n), where bi is the i-th element of b.
Output
Print the lexicographically minimum possible array c. Recall that your task is to reorder elements of the array b and obtain the lexicographically minimum possible array c, where the i-th element of c is ci=(ai+bi)%n.
example
Input
4
0 1 2 1
3 2 1 1
Output
1 0 0 2
这个题是在a数组不变的情况下从a数组中第一个数开始寻找b数组中的元素,使得(a[i]+b[i])%n最小。b数组中每个元素只能用一次,如果有多种情况,则输出字典序最小的即可。由于b数组中可能有重复的元素,此时就要使用multiset了。
AC代码如下:
#include<iostream>
#include<cstdio>
#include<string>
#include<cstring>
#include<set>
#include<algorithm>
using namespace std;
int a[200005];
int b[200005];
int c[200005]; //建立c数组存放与a每位相知对应的b中的数
int main()
{
int T;
cin>>T;
multiset<int>s; //建立一个不去重的set,即multiset
for(int i=0;i<T;i++)
{
cin>>a[i];
}
for(int i=0;i<T;i++)
{
cin>>b[i];
s.insert(b[i]); //将元素插入multiset中
}
int x=0;
for(int i=0;i<T;i++)
{
int k=T-a[i]; //计算出k值为最适合的值
multiset<int>::iterator it;
it=s.lower_bound(k); //寻找与a[i]相差大于等于k的最小值
if(it==s.end())
it=s.begin(); //如果没有找到,则将set中最小的元素赋给他
c[x]=*it;
s.erase(it);
//赋值完之后将地址清空删掉该元素,删除地址不删变量,因为变量可能会重复
x++;
}
for(int i=0;i<T;i++)
{
cout<<(a[i]+c[i])%T<<" ";
}
cout<<endl;
return 0;
}
拓展:关于equal_range()与pair的使用:
equal_range()使用的时候返回的是两个迭代器,此时应该使用pair,我感觉不太好用,估计是我太菜了,用不明白。而且,equal_range()其实是lower_bound()与upper_bound()的一起使用,这里介绍下pair的使用。
#include<iostream>
#include<set>
#include<algorithm>
using namespace std;
int main()
{
set<int>p;
p.insert(2);
p.insert(3);
p.insert(5);
p.insert(10);
p.insert(12);
int k;
cin>>k;
pair<set<int>::iterator,set<int>::iterator>it;
//这里利用pair建立了一对set的迭代器
it=p.equal_range(k);
cout<<*it.first<<" "<<*it.second<<endl;
return 0;
}
input:
4
output:
5 5
input:
3
output:
3 5
不知道为啥,感觉⑧太行。。。。