STL二次学习 列表
vector string queue priority_queue stack deque set,map,multiset,multimap unordered_set,unordered_map,unordered_multiset,unordered_multimap bitset
系统为某一程序分配空间时,所需时间与空间大小无关,与申请次数有关
vector #define _CRT_SECURE_NO_WARNINGS 1 #include <iostream> #include <cstring> #include <algorithm> #include <vector> using namespace std;int main () { int n = 10 ; int m = 3 ; vector<int > a; vector<int > a (n) ; vector<int > a (n, m) ; vector<int > a[10 ]; int k = a.size (); k = a.empty (); a.clear (); k = a.front (); k = a.back (); a.push_back (k); a.pop_back (); k = a.begin (); k = a.end (); return 0 ; }
代码示范
#define _CRT_SECURE_NO_WARNINGS 1 #include <iostream> #include <vector> using namespace std;int main () { 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 (vector<int >::iterator i = a.begin (); i != a.end ();++i) cout << *i << ' ' ; cout << endl; for (auto x : a) cout << x << ' ' ; cout << endl; return 0 ; }
vector支持比较运算,按字典序比较
#include <iostream> #include <vector> using namespace std;int main () { vector<int > a (4 , 3 ) ; vector<int > b (3 , 4 ) ; printf ("%d\n" ,a < b); return 0 ; }
pair 存储二元组
#define _CRT_SECURE_NO_WARNINGS 1 #include <iostream> #include <cstring> #include <vector> using namespace std;int main () { pair<int , string> p; p = make_pair (10 , "abc" ); p = { 20 ,"abc" }; int k = p.first; string s = p.second; pair<int , pair<int , int >> pp; }
也能够支持比较运算,按字典序比较,以first为第一关键字,second为第二关键字
string #define _CRT_SECURE_NO_WARNINGS 1 #include <iostream> #include <string> using namespace std;int main () { string s = "abc" ; s += "def" ; s += 'g' ; cout << s.substr (0 , 3 ) << endl; cout << s.substr (0 , 100 )<<endl; cout << s.substr (0 )<<endl; printf ("%s" , s.c_str ()); }
substr的第二个参数代表长度
queue #define _CRT_SECURE_NO_WARNINGS 1 #include <iostream> #include <queue> using namespace std;int main () { queue <int > q; for (int i = 0 ;i < 10 ;++i) q.push (i); for (int i = 0 ; i < 10 ;++i) { cout << q.front () << ' ' ; q.pop (); } cout << endl; for (int i = 0 ;i < 10 ;++i) q.push (i); cout<<q.back ()<<endl; q = queue <int >(); }
queue没有clear函数
priority_queue 优先队列,即堆,默认大根堆
#define _CRT_SECURE_NO_WARNINGS 1 #include <iostream> #include <queue> using namespace std;int main () { priority_queue<int > pq; for (int i = 0 ;i <= 10 ;++i) pq.push (i); for (int i = 0 ;i < 10 ;++i) { cout << pq.top ()<<' ' ; pq.pop (); } return 0 ; }
改成小根堆的两种方法
变号法
#define _CRT_SECURE_NO_WARNINGS 1 #include <iostream> #include <queue> using namespace std;int main () { priority_queue<int > pq; for (int i = 10 ;i >= 0 ;--i) pq.push (-i); for (int i = 0 ;i < 10 ;++i) { cout << -pq.top ()<<' ' ; pq.pop (); } return 0 ; }
定义法
#define _CRT_SECURE_NO_WARNINGS 1 #include <iostream> #include <queue> #include <vector> #include <algorithm> using namespace std;int main () { priority_queue<int , vector<int >, greater<int >> heap; for (int i = 10 ;i >= 0 ;--i) heap.push (i); for (int i = 0 ;i < 10 ;++i) { cout << heap.top () << ' ' ; heap.pop (); } return 0 ; }
stack 栈
#define _CRT_SECURE_NO_WARNINGS 1 #include <iostream> #include <stack> using namespace std;int main () { stack<int > s; for (int i = 0 ;i < 10 ;++i) s.push (i); for (int i = 0 ;i < 10 ;++i) { cout << s.top () << ' ' ; s.pop (); } return 0 ; }
deque 双端队列,加强版vector
#define _CRT_SECURE_NO_WARNINGS 1 #include <iostream> #include <deque> using namespace std;int main () { deque<int > q; int k = q.size (); k = q.empty (); q.clear (); k = q.front (); k = q.back (); q.push_back (k); q.pop_back (); q.push_front (k); q.pop_front (k); }
set/multiset set定义数组不允许出现重复元素,如果出现就会忽略掉
multiset是可以有重复元素的
所有set操作的时间复杂度都为logN
#define _CRT_SECURE_NO_WARNINGS 1 #include <iostream> #include <set> using namespace std;int main () { set<int > s; multiset <int > Ms; for (int i = 0 ;i < 10 ;++i) s.insert (i); cout << *s.find (2 ) << endl; cout << s.count (1 ) << endl; s.erase (1 ); set<int >::iterator i = s.end (); s.erase (i); int x = 1 ; i = s.lower_bound (x); i = s.upper_bound (x); return 0 ; }
迭代器里两个名词的意思
map/multimap map存储的是一种映射关系
同样可以用insert和erase
insert可以用pair插入
erase的参数可以是pair也可以是迭代器
find操作也和set一样
set操作能做的map大多都能做
实用操作,可以让任意类型作为下标,(时间复杂度为O(logN))
#define _CRT_SECURE_NO_WARNINGS 1 #include <iostream> #include <map> using namespace std;int main () { map<string, int > a; a["abc" ] = 1 ; cout << a["abc" ] << endl; return 0 ; }
unordered 优化set/multiset和map/multimap的时间复杂度,基于哈希表,使大多数操作的时间复杂度都为O(1)
但不支持lower_bound和upper_bound
,也不支持迭代器相关的操作
bitset 可以比正常的内存占用省掉八倍
c++中布尔数组为1字节,1024个字节也就是1kb内存
如果用bitset压位的话,让布尔数组每一个位都能被充分利用,那么就只需128个字节
bitset也支持位运算操作,也支持count操作