Some basic usage of STL commonly used in C + +

C++STL

1. vector

• Description: variable length array with multiplication idea

• Function:

1. size(): returns the number of elements (almost all containers have)
2. empty(): Returns whether it is empty (almost all containers have)
3. clear(): clear
4. front()/back(): returns the first / last element in the vector
5. push_back()/pop_back(): insert an element after the vector / delete the last element of the vector
6. 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:

1. First: first element
2. 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:

1. size(): returns the length of the string
2. empty(): Returns whether the string is empty
3. clear(): clear string
4. substr(): returns a substring. The first parameter is the starting position and the second parameter is the length (subscript starts from 0)
5. 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:

1. size(): returns the number of elements (almost all containers have)
2. empty(): Returns whether it is empty (almost all containers have)
3. push(): inserts an element at the end of the queue
4. front(): returns the queue header element
5. back(): returns the end of queue element
6. 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:

1. size(): returns the number of elements (almost all containers have)
2. empty(): Returns whether it is empty (almost all containers have)
3. push(): insert an element
4. top(): returns the heap top element
5. 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:

1. size(): returns the number of elements (almost all containers have)
2. empty(): Returns whether it is empty (almost all containers have)
3. push(): inserts an element into the top of the stack
4. top(): returns the stack top element
5. 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:
1. size(): returns the number of elements (almost all containers have)
2. empty(): Returns whether it is empty (almost all containers have)
3. clear(): clear
4. front()/back(): returns the first / last element in deque
5. push_back()/pop_back(): insert the last element of deque / delete the last element of vector
6. push_front()/pop_front(): insert an element to the front of deque / delete the front of deque
7. 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:

1. size(): returns the number of elements (almost all containers have)
2. empty(): Returns whether it is empty (almost all containers have)
3. clear(): clear
4. begin()/end(): returns the iterator of the first element of the container / returns the iterator of the last element of the vector
• set/multiset:

1. insert(): inserts a number
2. find(): find a number
3. count(): returns the number of a number
4. 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
5. 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:

1. insert(): the number of inserts is a pair
2. erase(): the input parameter is pair or iterator
3. find()
4. [] (time complexity is O(log n))
5. 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:
1. ~，&，|，^
2. >>,<<
3. ==,!=
4. []
5. count() returns the number of 1s
6. any() determines whether there is at least one 1
7. none() determines whether all are 0
8. set() sets all positions to 1
9. set(k, v) changes the k-th bit to v
10. reset() changes all bits to 0
11. flip() is equivalent to~
12. flip() reverses the k-th bit

Tags: C++ api STL

Posted on Sun, 31 Oct 2021 17:52:04 -0400 by brbsta