You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

95 lines
3.7 KiB

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

1、vector: 变长数组,数组长度动态的变化,倍增的思想。
vector<int> a; //初始化方式
vector<int> a(n); //长度为n的
vector<int> a(n,3); //长度为n,每个元素值是3。
a.size() //返回元素的个数
a.empty() //数组是不是空的时间复杂度是O(1)的。有变更保存的维护的所以是O(1)的,其它容器也是有这两个的。
a.clear() //清空 这个并不是所有容器都有clear的比如队列就没有这个。
a.front() //返回第一个数
a.back() //返回最后一个数
a.push_back() //数组插入一个数
a.pop_back() //删除数组末尾元素
a.begin() //迭代器寻址开始
a.end() //迭代器寻址结束
两种迭代方式
vector<int> a;
for(int i=0;i<10;i++) a.push_back(i);
for(int i=0;i<a.size();i++) cout<<a[i]<<" ";
cout<<endl;
for(auto i=a.begin();i!=a.end();i++) cout<<*i<<" "; // auto --->vector<int>::iterator
cout<<endl;
c++ 11专享效率高代码短
for(auto x:a) cout<<x<<" ";
cout<<endl;
黑科技:支持比较运算
vector<int> a(4,3),b(3,4);
if(a<b) puts("a<b"); //字典序对比
2、pair<int,string> p 二元组
p.first 第一个元素
p.second 第二个元素
支持比较运算,默认是按字典序
p=make_pair(10,"yxc");
p={10,"yxc"}; //用于保存两种属性,是一个整体,把要排序的放到 first里把不要排序的放在second里。如果一个东西有三种东西也可以用pair来存储
pair<int,pair<int,int>>
3、string: c++处理字符串的利器, substr()c_str()返回对应的字符数组的头指针。
size(),clear(),empty(),a+='c'
cout<<a.substr(下标开始位置,长度)<<endl; //注意这里是长度不是下标的停止位置其它语言都是截止位置C++ NB!
4、queue: 队列push(),pop(),front(),back(),size(),但是queue没有clear函数
q=queue<int>();可以达到清空q的效果
5、priorty_queue:优先队列,push(),top(),pop() 是堆的概念,这个玩意很常用,拿堆来实现的。
//降序队列,大顶堆(默认)
priority_queue <int,vector<int>,less<int> >q;
//升序队列,小顶堆
priority_queue <int,vector<int>,greater<int> > q;
6、stack: 栈push(),pop(),top(),empty(),size()
7、deque: 双端队列,队头队尾都可以插入删除,支持随机访问,相当于加强版的vector
size(),empty(),clear(),front(),back(),push_back(),pop_back(),push_front(),pop_front(),这TM也太强了
还支持随机寻址,但比一般的数组慢几倍
8、set,multiset,map,multimap:基于平衡二叉树(红黑树),动态维护有序序列。
size(),insert(),find():如果不存在返回a.end迭代器,count()返回某一个数的个数
set里面不能有重复元素,multiset是可以有重复元素的
erase(x)
(1)如果是一个数,那么删除所有等于这个数的节点
(2)如果是一个迭代器,那么删除这个迭代器
lower_bound()/upper_bound()
lower_bound():返回大于等于x的最小的数
upper_bound():返回大于x的最小的数
map/multimap
insert() 插入的数是一个pair
erase() 输入的参数是pair或者迭代器
find()
9、unordered_set,unordered_map,unordered_multiset,unordered_multimap:哈希表
10、bitset:压位
比如一个整数4个字节32位只需要知道每一位上的是0还是1.省空间是最牛的。能省8倍的空间。
bitset<10000> s;
~s 取反
&s 与
|s 或
^s 异或
>>,<<,==,!=,[],count(),返回有多少个1, any()判断是不是最少有1个1.
none()判断是不是全为0
set() 把所有位置成1.
set(k,v) 将第k位变成v.
reset() 把所有位置成0.
flip() 把所有位取反
flip(k) 把第k位取反
跳表老师说他也不会,没啥用。