通关数据结构 day_11 -- STL
STL
vector, 变长数组,倍增的思想
size() 返回元素个数
empty() 返回是否为空
clear() 清空
front()/back()
push_back()/pop_back()
begin()/end()
[]
支持比较运算,按字典序pair<int, int>
first, 第一个元素
second, 第二个元素
支持比较运算,以first为第一关键字,以second为第二关键字(字典序)string,字符串
size()/length() 返回字符串长度
empty()
clear()
substr(起始下标,(子串长度)) 返回子串
c_str() 返回字符串所在字符数组的起始地址queue, 队列
size()
empty()
push() 向队尾插入一个元素
front() 返回队头元素
back() 返回队尾元素
pop() 弹出队头元素priority_queue, 优先队列,默认是大根堆
size()
empty()
push() 插入一个元素
top() 返回堆顶元素
pop() 弹出堆顶元素
定义成小根堆的方式:priority_queue<int, vector, greater > q; stack, 栈
size()
empty()
push() 向栈顶插入一个元素
top() 返回栈顶元素
pop() 弹出栈顶元素deque, 双端队列
size()
empty()
clear()
front()/back()
push_back()/pop_back()
push_front()/pop_front()
begin()/end()
[]set, map, multiset, multimap, 基于平衡二叉树(红黑树),动态维护有序序列
size()
empty()
clear()
begin()/end()
++, -- 返回前驱和后继,时间复杂度 O(logn)set/multiset
insert() 插入一个数
find() 查找一个数
count() 返回某一个数的个数
erase()
(1) 输入是一个数x,删除所有x O(k + logn)
(2) 输入一个迭代器,删除这个迭代器
lower_bound()/upper_bound()
lower_bound(x) 返回大于等于x的最小的数的迭代器
upper_bound(x) 返回大于x的最小的数的迭代器map/multimap
insert() 插入的数是一个pair
erase() 输入的参数是pair或者迭代器
find()
[] 注意multimap不支持此操作。 时间复杂度是 O(logn)
lower_bound()/upper_bound()unordered_set, unordered_map, unordered_multiset, unordered_multimap, 哈希表
和上面类似,增删改查的时间复杂度是 O(1)
不支持 lower_bound()/upper_bound(), 迭代器的++,--bitset, 圧位
bitset<10000> s;
~, &, |, ^
>>, <<
==, !=
[] count() 返回有多少个1
any() 判断是否至少有一个1
none() 判断是否全为0 set() 把所有位置成1
set(k, v) 将第k位变成v
reset() 把所有位变成0
flip() 等价于~
flip(k) 把第k位取反
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream>
#include<vector>
#include<queue>
#include<stack>
#include<deque>
#include<set>
#include<map>
#include<unordered_map>
#include<unordered_set>
#include <bitset>
using namespace std;
int main()
{
cin.tie(0);
ios::sync_with_stdio(false);
//vector
cout << "vector" << endl;
vector<int> a;
vector<int> b(10);
vector<int> c(10,3); //有10个数,每一个数都是3
vector<int> d[10]; //定义 10 个 vector
cout << b.size() << endl; //元素的个数
cout << b.empty() << endl; //是不是空的
b.clear(); //清空
cout << b.size() << endl;
a.push_back(1); //向最后插入一个数
a.push_back(2);
cout << a.front() << endl; //第一个数
cout << a.back() << endl;
// a.begin()/a.end() 迭代器,第一个位置和最后一个位置,可以看成指针
for(auto i = a.begin();i != a.end();i++)
{
cout << *i << endl;
}
for(auto x:a)
{
cout << x << endl;
}
a.pop_back(); //删掉最后一个数
cout << *(a.begin()) << endl;
//支持比较运算,根据字典序来比较
vector<int> aa(4,3),bb(3,3);
if(aa > bb)
{
cout << "Yes" << endl;
}
cout << endl;
//pair
cout << "pair:" << endl;
pair<int,string> p;
p={20,"abc"};
cout << p.first << " " << p.second << endl;
pair<int,pair<int,int>> pp;
pp={30,{15,45}};
cout << pp.first << " " << pp.second.first << " " << pp.second.second << endl;
cout << endl;
//string
cout << "string:" << endl;
string str = "yxc";
str += 'c';
cout << str << endl;
str += "ccc";
cout << str << endl;
string str1 = str.substr(0,10); //超出就全部
string str2 = str.substr(1); //从 1 开始的 字串
cout << str2 << endl;
cout << str1 << endl;
cout << str.substr(0,3) << endl; //从 0 开始 3 个 字符
cout << str.c_str() << endl;
cout << endl;
//queue
cout << "queue:" << endl;
queue<int> q;
q.push(1); //向队尾插入元素
q.push(2);
cout << q.front() << endl; //返回队头元素
cout << q.back() << endl; //返回队尾元素
q.pop(); //弹出队头元素
cout << q.front() << endl;
q = queue<int>(); //重新构造一个q
cout << endl;
//priority_queue
cout << "priority_queue:" << endl;
priority_queue<int> heap; //默认大根堆
priority_queue<int,vector<int>,greater<int>>heap1; //小根堆
heap.push(2); //插入一个元素
cout << heap.top() << endl; //返回堆顶元素
heap.push(3);
cout << heap.top() << endl;
heap.push(1);
cout << heap.top() << endl;
heap.pop(); // 弹出堆顶元素
cout << heap.top() << endl;
cout << endl;
//stack
cout << "stack:" << endl;
stack<int> stk;
stk.push(1); //栈顶插入一个元素
stk.push(2);
cout << stk.top() << endl; //返回栈顶元素
stk.pop(); //弹出栈顶元素
cout << stk.top() << endl;
cout << endl;
//deque
cout << "deque:" << endl;
deque<int> dq;
dq.push_back(1);
dq.push_front(2);
cout << dq[1] << endl;
cout << dq.front() << endl;
cout << dq.back() << endl;
dq.pop_back();
cout << dq.back() << endl;
dq.pop_front();
cout << dq.front() << endl;
cout << endl;
//set&multiset
cout << "set&multiset:" << endl;
set<int> s; //不可重复
multiset<int> ms; //可以重复
s.insert(1);
s.insert(1);
cout << s.size() << endl;
ms.insert(1);
ms.insert(1);
cout << ms.size() << endl;
cout << s.count(1) << endl; //返回某一个数的数量
cout << ms.count(1) << endl;
s.erase(1);
cout << s.count(1) << endl;
s.insert(1);
s.insert(3);
s.insert(2);
set<int>::iterator it;
it = s.lower_bound(3); //返回大于等于 x 的最小的数的迭代器
cout << *it << endl;
it = s.upper_bound(3); //返回大于 x 的最小的数的迭代器
cout << *it << endl;
cout << endl;
//map&multimap
cout << "map&multimap:" << endl;
map<int,int> map;
multimap<int,int> mlmap; //可重复
map[1] = 1;
map.insert({2,2});
map[3] = 3;
cout << map[1] << endl;
cout << map[2] << endl;
cout << map.size() << endl;
auto mapit = map.begin();
map.erase(mapit);
cout << map.size() << endl;
cout << endl;
//unordered_set&unordered_map&unordered_multimap&unordered_multiset
//不支持 lower_bound & upper_bound,其余同上
// crud 复杂度是O(1)
//bitset
// 假设我们需要开一个 1024个bool 的数组,那么我们需要 1k的内存
// 如果我们把他压到每一位的话,我们只需要 128B 的内存
cout << "bitset:" << endl;
// 存 10000x10000的bool矩阵
bitset<10000> bit;
cout << bit.any() << endl; //判断是否至少有一个 1
bit.set(); //把所有位变成 1
//~bit; 取反
cout << bit.any() << endl; //判断是否至少有一个 1
cout << bit.none() << endl;
bit.reset(); //把所有位变成 0
cout << bit.none() << endl; //判断是否全为 0
bit.set(0,1); //把第 0 位变成 1
bit.flip(); //等价于 ~
bit.flip(0); //把第 0 位取反
return 0;
}