文章目录
pair
分块
1. 初始化
pair <int , int > a ;
pair <int , int > b(2,4) ;
2. 调用
cout << a.fi << " " << a.se << endl ;
make_pair(x,y) ;
3.cmp规则
cmp :: a . f i < b . f i a.fi < b.fi a.fi<b.fi
a . f i = = b . f i − − − − − − > > > > a . s e < b . s e a.fi == b.fi ------>>>> a.se < b.se a.fi==b.fi−−−−−−>>>>a.se<b.se
板子
#define fi first
#define se second ;
pair <int , int > a ;
pair <int , int > b(2,4) ;
signed main (){
cout << a.fi << " " << a.se << endl ;
auto t = make_pair(1,2) ;
// cmp :: a.fi < b.fi ? || a.fi == b.fi a.se < b.se
}
array
STL中的可以当作单独数据结构的定长数组。
1.初始化
array<int , 5 > ay ;
2. 调用
cout << a[0] << a[1] << ... ;
auto [x,y,z] = a ; // C++17
3.CMP
先比前面的,再比后面的
A1 <> B1 – > A2 <> B2 …
STACK 栈
1. 初始化
#include <stack>
stack <int> stk ;
2.调用
stk.push(1) ;
stk.pop() ;
stk.top() ;
3.大小
stk.size() ;
stk.empty() ;
Queue 队列
1. 初始化
#include <queue>
queue <int> q ;
2. 调用
q.push(1) ;
q.pop() ;
cout << q.front() << " " << q.back() << endl ;
3. 大小
cout << q.empty() << endl ; // empty == true
cout << q.size() << endl ;
Dequeue 双端队列
双端队列和单端队列唯一不一样的地方就是可以分别对前后端进行操作
1.初始化
#include <dequeue>
dequeue <int> deq
2. 调用
v.push_back(1) ; v.push_front(0) ;
v.pop_back() , v.pop_back() ;
cout << v.front() << " " << v.back() << endl ;
3. 大小
cout << q.empty() << endl ; // empty == true
cout << q.size() << endl ;
Vector 动态数组
分解
1.初始化
#include <vector>
vector <int> vec ;
vector <int> v(5) ;
vector <int> v1(6,100) ;
vector <int> v2{1,2,3} ;
2. 调用
v.push_back(1) ;
v.pop_back() ;
cout << v.front() << " " << v.back() << endl ;
cout << vec[i] << endl ;
v.insert(迭代器,value) // 插在迭代器的右侧
v.erase(迭代器) // 删除当前迭代器位置上的内容
3. 大小
cout << q.empty() << endl ; // empty == true
cout << q.size() << endl ;
4.进阶
vec.clear() // 清空操作 时间复杂度O(N)
// sort
#include <algorithm>
sort(a.beign() , a.end() ) ; // O(NlogN)
auto lower = lower_bound(v.begin() , v.end() , 100) ; // <= 100 的指针 , 第一个位置 return it , if none return vec.end() ;
auto upper upper_bound(v.begin() , v.end() , 100) ; // < 100 的指针 , 第一个位置 return it
// return 指针. O(logN)
5.迭代器
v.beign() , v.end() // end -> 最后一位的下一位
vector <int> :: iterator it = 迭代器
it ++ ; it -- , it = prev(it) ;
模板
#include <iostream>
#include <vector>
#include <algorithm>
#define SZ(x) ((int)x.size())
using namespace std ;
vector <int> v(3 , 100) ;
vector <int> v1{3 , 100} ;
vector <int> v2(34) ;
vector <int> v3 ;
signed main (){
v.push_back(1) ;
v.pop_back() ;
cout << v.front() << " " << v.back() << endl ;
cout << vec[i] << endl ;
v.insert(迭代器,value) // 插在迭代器的右侧
v.erase(迭代器) // 删除当前迭代器位置上的内容
cout << q.empty() << endl ; // empty == true
cout << q.size() << endl ;
vec.clear() // 清空操作 时间复杂度O(N)
sort(a.beign() , a.end() ) ;
auto lower = lower_bound(v.begin() , v.end() , 100) ; // <= 100 的指针 , 第一个位置 return it , if none return vec.end() ;
auto upper upper_bound(v.begin() , v.end() , 100) ; // < 100 的指针 , 第一个位置 ret urn it
// return 指针.
/* 迭代器 :
v.beign() , v.end() // end -> 最后一位的下一位
vector <int> :: iterator it = 迭代器
it ++ ; it -- , it = prev(it) ;
*/
return 0 ;
}
Set 集合
1.初始化
#include <set>
set <int> s ; // 去重
set <int> s1(v.begin(),v.end()) ; // 迭代器初始化
2. 调用
s.insert(x) ; // O(logN)
s.erase(x) ; // O(logN)
3. 大小
cout << q.empty() << endl ; // empty == true
cout << s.size() << endl ;
4. find
s.find() // 找到返回迭代器,找不到返 s.end() O(logN)
s.count() // 是否为 true ; O(logN)
s.lower_bound(x) // logN
s.upper_bound(x) // logN
Map 咉射数组
1.初始化
#include <map>
map<int> mp ; // 去重
2. 调用
mp.insert(make_pair(keys,value)) ; // log(N)
mp[''] = '' //log(N)
mp.erase(keys) ; // log(N)
3. 大小
cout << mp.empty() << endl ; // empty == true
cout << mp.size() << endl ;
4. find
mp.find() // 找到返回迭代器,找不到返 s.end()
mp.count() // 是否为 true ;
s.lower_bound(x) // logN
s.upper_bound(x) // logN
// 返回的时键值对
Priority_queue 优先队列
1.初始化
priority_queue <int> q ; // big_queue
priority_queue<int , vector<int> , greater<int> > v ; // smart_queue
2.调用
q.push(1) ; // log(N)
q.pop() ; // log(N)
cout << q.top() << endl ;
3. 大小
cout << q.empty() << endl ; // empty == true
cout << q.size() << endl ;
Multiset 集合
multiset 和 set ,区别是 multiset 会存储数的个数。
1.初始化
#include <multiset>
multisetset <int> s ; // 去重
multisetset <int> s1(v.begin(),v.end()) ; // 迭代器初始化
2. 调用
s.insert(x) ; // log(N)
s.erase(x) ; // 删掉所有的 , 但是如果是迭代器的话可以删去一个
3. 大小
cout << q.empty() << endl ; // empty == true
cout << s.size() << endl ;
4. find
s.find() // 找到返回迭代器,找不到返 s.end() log(N)
s.count() // 是否为 true ; log(N)
unordered_set , unordered_map哈希表实现的set 与 map
哈希实现,对所有 set , map 的操作都能 O(1) 实现,但是容易会被卡hash