目录
unordered_set, unordered_map, unordered_multiset, unordered_multimap, 哈希表
vector容器
变长数组,倍增思想
size() 返回元素个数
empty() 返回是否为空
clear() 清空
front()/ back()
push_back() / pop_back()
begin() / end()
[]
支持比较运算
#include <vector>
#include <iostream>
using namespace std;
int main()
{
vector<int> a;
for(int i = 1; i <= 10; i ++)a.push_back(i);
//遍历
for(int i = 0; i < a.size(); i++)cout << a[i] << " ";
cout <<"\n";
for(vector<int>::iterator i = a.begin(); i != a.end(); i++)cout << *i <<" ";
cout << "\n";
for(auto e: a)cout << e << " ";cout <<"\n";
vector<int> aa(4, 3), bb(3, 4);
if(aa < bb){
puts("aa < bb");
for(auto e: aa)cout << e << " ";cout <<"\n";
}
return 0;
}
/*
1 2 3 4 5 6 7 8 9 10
1 2 3 4 5 6 7 8 9 10
1 2 3 4 5 6 7 8 9 10
aa < bb
3 3 3 3
*/
pair<int, int>
first 第一个元素
second 第二个元素
支持比较运算, 以first作为第一关键字,以second为第二个关键字(字典序)
#include <vector>
#include <iostream>
using namespace std;
int main()
{
pair<int, string> p, p2;
p = make_pair(10, "sms");
cout << p.first << " " << p.second << "\n";
p2 = {20, "aba"};
if(p < p2){
cout << p.first << "\n";
}
/*
10 sms
10
*/
return 0;
}
string 字符串
substr(), c_str()
size() / length()
empty()
clear()
#include <vector>
#include <iostream>
using namespace std;
int main()
{
string a = "*holla";
a += "!!";
a += '!';
cout << a << "\n";
cout << a.substr(1, 5) << "\n";
cout << a.substr(1) <<"\n";
printf("%s", a.c_str());// 不用直接用a
return 0;
}
/*
*holla!!!
holla
holla!!!
*holla!!!
*/
queue队列
size()
empty()
push() 向队尾插入一个元素
front() 返回队头元素
back() 返回队尾元素
pop() 弹出队头元素
#include <iostream>
#include <queue>
using namespace std;
int main()
{
queue<int> q;
q = queue<int>(); //没有clear()函数
q.push(6);
q.push(3);
cout << q.front() << " " << q.back() << "\n";//6 3
cout << q.size() ;//2
return 0;
}
priority_queue 优先队列
【默认大根堆】
push() 插入元素
top() 返回堆顶元素
pop() 弹出堆顶元素
#include <iostream>
#include <queue>
using namespace std;
int main()
{
priority_queue<int> heap;
heap. push(3);
heap.push(9);
cout << heap.top() << "\n"; //9
priority_queue<int, vector<int>, greater<int> > h; // 小根堆
//或者插入的时候乘以 -1
return 0;
}
stack 栈
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(log N)
set / multiset:
insert() 插入一个数
find()
count()
erase()
1)输入是一个数x, 删除所有 x ; 复杂度O(k + log N)
2)输入一个迭代器,删除这个迭代器
lower_bound() / upper_bound()
lower_bound() 返回大于等于x的最小的数的迭代器
upper_bound() 返回大于x的最小的数的迭代器
map/ multimap
insert() 插入的数是一个pair
erase() 输入的参数是pair或者迭代器
find()
[ ] 时间复杂度是O(log n)
lower_bound() / upper_bound()
#include <iostream>
#include <map>
using namespace std;
int main()
{
map<string, int> a;
a["holla"] = 1;
cout << a["holla"] << "\n"; // 1
return 0;
}
unordered_set, unordered_map, unordered_multiset, unordered_multimap, 哈希表
size()
empty()
clear()
begin() / end() ++, -- 返回前趋和后继,时间复杂度 O(log N)
unordered_set / unordered_multiset:
insert() 插入一个数
find()
count()
erase()
1)输入是一个数x, 删除所有 x ; 复杂度O(k + log N)
2)输入一个迭代器,删除这个迭代器
unordered_map/ unordered_multimap
insert() 插入的数是一个pair
erase() 输入的参数是pair或者迭代器
find()
[ ] 时间复杂度是O(log n)
和上面类似,增删改查的时间复杂度是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位取反
以下两篇文章详细讲述了数据结构相关算法。
数据结构【上】_萨曼塔的博客-CSDN博客https://blog.csdn.net/weixin_63544745/article/details/132678153
数据结构【下】-CSDN博客https://blog.csdn.net/weixin_63544745/article/details/133130730