C++STL
1. vector
-
Description: variable length array with multiplication idea
-
Function:
- size(): returns the number of elements (almost all containers have)
- empty(): Returns whether it is empty (almost all containers have)
- clear(): clear
- front()/back(): returns the first / last element in the vector
- push_back()/pop_back(): insert an element after the vector / delete the last element of the vector
- begin()/end(): returns the position of the first element of the vector / returns the position after the last element of the vector
-
Supplement: it can be accessed using [] like an array
-
use:
#include <iostream> #include <cstdio> #include <algorithm> #include <cstring> #include <vector> using namespace std; int main(void) { vector<int> a(10, -1); // Define a vector with a length of 10 and assign each value to - 1 a.size(); // Container length, O(1) all containers have a.empty(); // Whether the container is empty, O(1) all containers have for (auto x : a) cout << x << endl; vector<int> b; for (int i = 1; i < 10; ++i) b.push_back(i); for (int i = 0; i < b.size(); ++i) cout << b[i] << endl; // The first traversal method for (vector<int>::iterator i = b.begin(); i != b.end(); ++i) cout << *i << endl; // The second traversal method for (auto x : b) cout << x << endl; // The third traversal mode is a new traversal mode in C++11 return 0; }
2. pair entered by mistake
-
Brief description: integrate two data into one data
-
use:
- First: first element
- Second: the second element (comparison operation is supported, with first as the first keyword and second as the second keyword)
#include <iostream> #include <cstdio> #include <algorithm> #include <cstring> #include <vector> using namespace std; int main(void) { pair<int, string> p; // There are two ways to create pair s p = make_pair(1, "moyan"); p = { 1, "moyan" }; // C++11 cout << p.first << " " << p.second << endl; return 0; }
3. string
-
Description: String
-
Function:
- size(): returns the length of the string
- empty(): Returns whether the string is empty
- clear(): clear string
- substr(): returns a substring. The first parameter is the starting position and the second parameter is the length (subscript starts from 0)
- c_str(): returns a C language style string, which is used for printf to output string type strings
-
use:
#include <iostream> #include <cstdio> #include <algorithm> #include <cstring> #include <vector> #include <string> using namespace std; int main(void) { string a = "abc"; a += "def"; cout << a << endl; cout << a.substr(1, 2) << endl; printf("%s\n", a.c_str()); return 0; }
4. queue
-
Brief description: queue, first in first out
-
Function:
- size(): returns the number of elements (almost all containers have)
- empty(): Returns whether it is empty (almost all containers have)
- push(): inserts an element at the end of the queue
- front(): returns the queue header element
- back(): returns the end of queue element
- pop(): pop up the queue header element
-
use:
#include <iostream> #include <cstdio> #include <algorithm> #include <cstring> #include <vector> #include <queue> using namespace std; int main(void) { queue<int> q; q.push(1); q.push(2); q.push(3); while (!q.empty()) { int tmp = q.front(); q.pop(); cout << tmp << endl; } return 0; }
5. priority_queue
-
Brief description: priority queue, also known as heap, defaults to large root heap.
-
Function:
- size(): returns the number of elements (almost all containers have)
- empty(): Returns whether it is empty (almost all containers have)
- push(): insert an element
- top(): returns the heap top element
- pop(): pop the top of heap element
-
use:
#include <iostream> #include <cstdio> #include <algorithm> #include <cstring> #include <vector> #include <queue> using namespace std; int main(void) { priority_queue<int> q; // Big root pile q.push(1); q.push(2); q.push(3); while (!q.empty()) { int tmp = q.top(); q.pop(); cout << tmp << endl; } priority_queue<int, vector<int>, greater<int> >heap; // Small root pile, increasing from top to bottom heap.push(1); heap.push(2); heap.push(3); while (!heap.empty()) { int tmp = heap.top(); heap.pop(); cout << tmp << endl; } return 0; }
6. stack
-
Description: stack, first in first out, also known as last in first out
-
Function:
- size(): returns the number of elements (almost all containers have)
- empty(): Returns whether it is empty (almost all containers have)
- push(): inserts an element into the top of the stack
- top(): returns the stack top element
- pop(): pop up stack top element
-
use:
#include <iostream> #include <cstdio> #include <algorithm> #include <cstring> #include <vector> #include <queue> #include <stack> using namespace std; int main(void) { stack<int> s; s.push(1); s.push(2); s.push(3); while (!s.empty()) { int tmp = s.top(); s.pop(); cout << tmp << endl; } return 0; }
7. deque
- Brief description: double ended queue, which can be called enhanced vector, is relatively slow, that is, the efficiency is low
- Function:
- size(): returns the number of elements (almost all containers have)
- empty(): Returns whether it is empty (almost all containers have)
- clear(): clear
- front()/back(): returns the first / last element in deque
- push_back()/pop_back(): insert the last element of deque / delete the last element of vector
- push_front()/pop_front(): insert an element to the front of deque / delete the front of deque
- begin()/end(): returns the position of the first element of the vector / returns the position after the last element of the vector
- Supplement: you can also use [] for random access
8. set,map,multiset,multimap
-
Description: Based on balanced binary tree (red black tree), dynamic maintenance sequence
-
Function:
- size(): returns the number of elements (almost all containers have)
- empty(): Returns whether it is empty (almost all containers have)
- clear(): clear
- begin()/end(): returns the iterator of the first element of the container / returns the iterator of the last element of the vector
-
set/multiset:
- insert(): inserts a number
- find(): find a number
- count(): returns the number of a number
- erase(): there is one parameter. If the parameter is a number x, delete all x in the container (time complexity O (K + logn), where k is the number of x). If parameter is an iterator, iterator is deleted
- lower_bound()/upper_bound(): the two core functions. lower_bound(x) returns the iterator that is greater than or equal to the minimum number of X, upper_bound(x) returns an iterator that is greater than or equal to the maximum number of X.
-
map/multimap:
- insert(): the number of inserts is a pair
- erase(): the input parameter is pair or iterator
- find()
- [] (time complexity is O(log n))
- lower_bound()/upper_bound()
#include <iostream> #include <cstdio> #include <algorithm> #include <cstring> #include <vector> #include <queue> #include <stack> #include <map> using namespace std; int main(void) { map<int, string> m; m.insert({ 1, "Zhang San" }); m.insert({ 2, "Li Si" }); cout << (*m.find(1)).second << endl; cout << m[1] << endl; return 0; }
9. unordered_set, unordered_map,unordered_multiset,unordered_multimap
- It is similar to the above set, map... But the addition, deletion, modification and query time is O(1) and lower is not supported_ bound/upper_ bound
- Implementation based on hash table
10. bitset
- Description: pressure level
- Usage: BitSet < 10000 > s (10000 is digits)
- The following operations are supported:
- ~,&,|,^
- >>,<<
- ==,!=
- []
- count() returns the number of 1s
- any() determines whether there is at least one 1
- none() determines whether all are 0
- set() sets all positions to 1
- set(k, v) changes the k-th bit to v
- reset() changes all bits to 0
- flip() is equivalent to~
- flip() reverses the k-th bit