vector,变长数组,倍增的思想
size() // 返回元素个数
empty() // 返回是否为空
clear() // 清空
front() / back() // 返回数组的第一个数/返回数组的最后一个元素
push_back() / pop_back() // 向数组的最后插入一个数 / 把数组的最后一个元素删掉
begin() / end() // begin是vector的第0个数/end是最后一个数的下一个数
支持比较运算,按字典序
1、定义
//最基本的定义
vector<int> a;
//定义元素数量为n的vector
vector<int> a(n);
//将数组所有元素设置为k
vector<int> a(n,k);
2、数据输入
vector<int> a;
for(int i = 0 ; i < n ; i ++) a.push_back(i);
3、遍历方法
vector<int> a;
//遍历方法1
for(int i = 0 ; i < a.size(); i ++) cout << a[i] << ' ';
cout << endl;
//遍历方法2:用vector的迭代器来遍历
//vector<int>::iterator 可以用auto代替它
for(vector<int>::iterator i = a.begin(); i != a.end() ; i ++) cout << *i << ' ';
cout << endl;
//遍历方法3:C++范围遍历:可遍历容器内所有元素,代码短,效率高
for(auto x : a) cout << x << ' ';
cout << endl;
4、比较运算
//注意是字典序
vector<int> a(4,3),b(3,4);
if(a < b) cout << "a < b" << endl;
pair<int, int>
first, 第一个元素
second, 第二个元素
支持比较运算,以first为第一关键字,以second为第二关键字(字典序)
string, 字符串
size() / length() // 返回字符串长度
empty()
clear()
substr(起始下标,(子串长度)) // 返回子串
c_str() // 返回字符串所在字符数组的起始地址
1、基本操作
#include<bits/stdc++.h>
using namespace std;
typedef unsigned long long ll;
int main()
{
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
string a = "yxc";
a += "def";
a += 'c';
cout << a << endl; // 输出yxcdefc
return 0;
}
2、返回子串
#include<bits/stdc++.h>
using namespace std;
typedef unsigned long long ll;
int main()
{
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
string a = "yxc";
a += "def";
a += 'c';
cout << a.substr(1,2) << endl; // 输出xc(返回起始坐标为1,长度为2的子串)
cout << a.substr(1) << endl; // 输出xcdefc(返回起始坐标为1,一直到字符串末尾的子串)
return 0;
}
3、用printf输出string
#include<bits/stdc++.h>
using namespace std;
typedef unsigned long long ll;
int main()
{
string a = "yxc";
a += "def";
a += 'c';
printf("%s",a.c_str());
return 0;
}
queue, 队列
size()
empty()
push() // 向队尾插入一个元素
front() // 返回队头元素
back() // 返回队尾元素
pop() // 弹出队头元素
priority_queue, 优先队列, 默认是大根堆
priority_queue, 优先队列, 默认是大根堆
size()
empty()
push() // 插入一个元素
top() // 返回堆顶元素
pop() // 弹出堆顶元素
定义成小根堆的方式: priority_queue<int, vector<int>>, greater<int>> 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()
++, -- // 返回前驱和后继,时间复杂度 0(logn)
set/multiset
insert() // 插入一个数
find() // 查找一个数
count() // 返回某一个数的个数
erase()
(1)输入一个数x,删除所有x 0(k + logn)
(2)输入一个迭代器,删除这个迭代器
lower_bound() / upper_bound()
(1)lower_bound(x) // 返回大于等于x的最小的数的迭代器
(2)upper_bound(x) // 返回大于x的最小的数的迭代器
map/multimap
insert() // 插入的数是一个pair
erase() // 输入的参数是pair或者迭代器
find()
[] // 注意multimap不知此此操作。时间复杂度是(logn)
lower_bound() / upper_bound()
unordered_set, unordered_map, unordered_multiset ,unordered_multimap, 没有顺序,基于哈希表实现
和上面类似,增删改查的时间复杂度是 0(1)
不支持 lower_bound() / upper_bound(), 迭代器的++, --
bitset, 压位
bitset<10000> s;
~, &, l, ^
>>, <<
==, !=
[]
count() //返回有多少个1
any() //判断是否至少有一个1
none() //判断是否全为0
set() //把所有位置成1
set(k,v) //将第k位变成v
reset() //把所有位变成0
flip() //等价于~
flip(k) // 把第k位取反