set 与 multiset 的区别: set 不能存相同元素, multiset 可以存相同的元素,其余的使⽤⽅式完全⼀致。因此,我们有时候可以⽤ set 帮助我们给数据去重。
#include
#include
using namespace std;
int main()
{
set mp1;
set mp2;
return 0;
}
迭代器,可以使⽤范围for遍历整个红⿊树。
遍历是按照中序遍历的顺序,因此是⼀个有序的序列
向红⿊树中插⼊⼀个元素
时间复杂度:O(log N)
删除⼀个元素
时间复杂度:O(log N)
#include
#include
using namespace std;
int a[] = {10, 60, 20, 70, 80, 30, 90, 40, 100, 50};
int main()
{
set mp;
// 插⼊
for(auto x : a)
{
mp.insert(x);
}
// 遍历 set,最终的结果应该是有序的
for(auto x : mp)
{
cout << x << " ";
}
cout << endl;
// if(mp.count(1)) cout << "1" << endl;
// if(mp.count(99)) cout << "99" << endl;
// if(mp.count(30)) cout << "30" << endl;
// if(mp.count(10)) cout << "10" << endl;
// mp.erase(30);
// mp.erase(10);
// if(mp.count(30)) cout << "30" << endl;
// else cout << "no:30" << endl;
// if(mp.count(10)) cout << "10" << endl;
// else cout << "no:10" << endl;
auto x = mp.lower_bound(20);
auto y = mp.upper_bound(20);
cout << *x << " " << *y << endl;
return 0;
}
map 与 multimap 的区别: map 不能存相同元素, multimap 可以存相同的元素,其余的使⽤⽅式完全⼀致。
map 与 set 的区别: set ⾥⾯存的是⼀个单独的关键字,也就是存⼀个int 、char 、double或者string 。⽽ map ⾥⾯存的是⼀个 pair
可以这样理解:红⿊树⾥⾯⼀个⼀个的结点都是⼀个结构体,⾥⾯有两个元素分别是 key 和 value 。插⼊、删除和查找的过程中,⽐较的是 key 的值。
⽐如,我们可以在 map 中:
,来统计数字出现的次数;
,来统计字符串出现的次数;
,表⽰⼀个字符串变成另⼀个字符串;>
来表⽰⼀个数后⾯跟了若⼲个数…,⽤来存储树。#include
#include
#include
迭代器,可以使⽤范围for遍历整个红⿊树。
遍历是按照中序遍历的顺序,因此是⼀个有序的序列
向红⿊树中插⼊⼀个元素。这⾥需要插⼊⼀个pair,可以⽤ {} 形式。⽐如: mp.insert({1, 2})
时间复杂度:O(log N)
重载[]
,使的map可以像数组⼀样使⽤。
这是map最好⽤的接⼝,有了这个重载,map的使⽤就变得特别轻松,不⽤写很多冗余的代码。
它的底层其实就是调⽤了insert函数,并且会返回val的引⽤。
删除⼀个元素。
时间复杂度:O(log N)
#include
#include
把所有高级词汇,含金量绑定,放在map中,读取英语作文,每读取一个英语单词的时候,就去map里找含金量
#include
using namespace std;
typedef long long LL;
int n, p;
map mp;
bool check(char ch)
{
if ((ch >= '0' && ch <= '9') || (ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch <= 'Z'))
{
return true;
}
else
return false;
}
int main()
{
//ios::sync_with_stdio(false);
//cin.tie(0);
cin >> n >> p;
for (int i = 1; i <= n; i++)
{
string s; int x;
cin >> s >> x;
mp[s] = x;
}
LL ret = 0;
char ch;
string t = "";
while (scanf("%c", &ch) != EOF)
{
if (check(ch)) t += ch;
else
{
ret = (ret + mp[t]) % p;
t = "";
}
}
cout << ret << endl;
return 0;
}
针对每一天的营业额x,找出前面的数中,哪个数离他最近
大于等于x的最小值y
小于等于x的最大值z
对y-x和z-x的绝对值取min
#include
using namespace std;
const int INF = 1e7 + 10;
int n;
set mp;
int main()
{
ios::sync_with_stdio(false);
cin.tie(0);
cin >> n;
int ret; cin >> ret;
mp.insert(ret);
//左右边界
mp.insert(-INF); mp.insert(INF);
for (int i = 2; i <= n; i++)
{
int x; cin >> x;
auto it = mp.lower_bound(x);
auto tmp = it;
tmp--;
if (*it == x) continue;
ret += min(abs(*tmp - x), abs(*it - x));
mp.insert(x);
}
cout << ret << endl;
return 0;
}
#include
using namespace std;
typedef long long LL;
const LL INF = 1e10 + 10;
set mp;
int main()
{
ios::sync_with_stdio(false);
cin.tie(0);
int q; cin >> q;
//处理边界
mp.insert(-INF); mp.insert(INF);
while (q--)
{
LL op, len; cin >> op >> len;
if (op == 1)
{
if (mp.count(len)) cout << "Already Exist" << endl;
else mp.insert(len);
}
else
{
//找最近的那个
if (mp.size() == 2)
{
cout << "Empty" << endl;
}
else
{
auto it = mp.lower_bound(len);
auto tmp = it;
tmp--;
if (abs(*tmp - len) <= abs(*it - len))
{
cout << *tmp << endl;
mp.erase(tmp);
}
else
{
cout << *it << endl;
mp.erase(it);
}
}
}
}
return 0;
}