Article Directory
C ++ Standard Template Library (Standard Template Library, referred to as STL) is a class library containers and algorithms, commonly used containers with a stack, queue, priorty_queue, set, vector, pair, string and the like, commonly used algorithms sort, max , min, abs, swap, reverse, etc.
All containers support methods
method | description | Examples | time complexity |
---|---|---|---|
size | Returns the number of elements of the container | s.size() | O (1) |
empty | Determining whether the container is empty, the container is empty, return true | s.empty() | O (1) |
Stack (stack)
It included in the stack header file
Features: You can only put in the top of the stack elements, remove elements in the stack, LIFO
method | description | Examples | time complexity |
---|---|---|---|
push | Stack (stack) | s.push(x) | O (1) |
pop | The stack (stack) | s.pop() | O (1) |
top | Returns the top element | s.top() | O (1) |
Circular queue (Queue)
It included in the queue header file
Features: You can only remove elements from the team heads into the elements from the tail, FIFO
method | description | Examples | time complexity |
---|---|---|---|
push | The team (the tail) | que.push (x) | O (1) |
pop | A team (team head) | que.pop () | O (1) |
front | Return the head elements | que.front () | O (1) |
back | Return the tail element | que.back () | O (1) |
Priority queue (The priority_queue)
It included in the queue header file
priority_queue large root can be understood as a binary heap that the top element is the maximum
Declare a value extracted in descending priority queue
priority_queue<int> que;//大根堆
A statement taken from small to large values of the priority queue
priority_queue<int, vector<int>, greater<int> > que;//小根堆
method | description | Examples | time complexity |
---|---|---|---|
push | The stack is inserted into the element x | que.push (x) | O (logn) |
pop | Delete the top of the heap element | que.pop () | O (logn) |
top | Return to top of the heap element | que.top() | O (1) |
Ordered set (set)
Included in the set header file
realized by a red-black tree set, the set of elements is not repeated, the function of automatic deduplication
method | description | Examples | time complexity |
---|---|---|---|
clear | Clear | s.clear() | O (n) |
insert | Insert elements x | s.insert(x) | O (logn) |
find | Returns an iterator elements equal to x. Elements equal to x if not, returns s.end () | s.find(x) | O (logn) |
erase | Delete iterator it points to the elements | s.erase(it) | O (logn) |
erase | Delete the equal elements of x | s.erase(x) | O (logn) |
count | The returned collection is equal to the number of elements x | s.count(x) | O (logn) |
Vector (vector)
Included in the vector header file
vector is a dynamic array, you can use like an array subscript to access elements
method | description | Examples | time complexity |
---|---|---|---|
clear | Clear | v.clear() | O (n) |
push_back | The element x into the vector tail | v.push_back(x) | O (1) |
pop_back | Removes the last element vector tail | v.pop_back | O (1) |
Dictionary (map)
It included in the map header file
map is associated with a container, to provide one key value (key-value) mapping
method | description | Examples | time complexity |
---|---|---|---|
clear | Clear | m.clear() | O (n) |
insert | Insert key-value pairs | m.insert(make_pair(key, value)) | O (logn) |
find | Find key for the tuple x and returns its iterator | m.find(x) | O (logn) |
[] Operator | Return value key is mapped to | m[key] | O (logn) |
[] Operator | For m [key] to assignment | m[key] = value | O (logn) |
Of the group (pair)
Utility included in the header file, but in general, iostream it also contains
method | description | Examples | time complexity |
---|---|---|---|
make_pair | Construct a pair group | make_pair(1, 2) | |
first | Returns the first element | p.first | O (1) |
second | Returns the second element | p.second | O (1) |
String (string)
Included in the string header file
string is a class, regardless of memory allocation problem, and provides a range of operational functions
operating | string | char* |
---|---|---|
definition | string s | char s[105] |
Get the first i characters | s[i] | s[i] |
Returns the length | s.size() | strlen(s) |
Reads one line | getline (eating, s) | gets(s) |
Assignment | s = “hello” | strcpy(s, “hello”) |
splice | s += “world” | strcat(s, “world”) |
Compare | s == “hello” | strcmp(s, “hello”) |
The sort function
It included in the algorithm header file
usage:
注意:
-
sort排序的范围是前闭后开,例如:sort(arr, arr+10); 排序的范围是从arr[0]到arr[9]
-
sort的排序方法不写的话,默认是从小到大
从大到小的排序方法
bool cmp(int a, int b)
{
return a > b;
}
max、min函数
max(x, y):返回x和y中的最大值
min(x, y):返回x和y中的最小值
abs函数
abs(x):返回x的绝对值,x为整数。
如果要返回浮点数的绝对值,要用math.h头文件下的fabs
swap函数
swap(x, y):交换x和y的值
reverse函数
reverse(a, b):可以将数组指针在[a, b)之间的元素或容器的迭代器在[a, b)范围内的元素进行反转。
翻转数组
reverse(arr, arr + n);
翻转string
reverse(s.begin(), s.end());