8、 Six components of C + + STL – a sharp programming tool you must know and know


For STL, it is recommended to directly read Chapters 9 to 11 of C + + primer, with a very detailed list of interfaces and many examples. There are also commonly used generic algorithms in the appendix, which are suitable for frequent review

Vector container

Underlying data structure: dynamically developed array, which is expanded by twice the size of the original space each time

vector vec;

Deque double ended queue and list linked list

The initial element is placed in the middle of the queue to facilitate the subsequent addition of elements. There is an external mapper storage queue. When the queue is full, the mapper will be expanded. The queue will be placed in the sizeof (original mapper) / 2 position of the expanded mapper.

Deque container:

List container

Vector, deque and list comparison

The difference between vecotr and deque?

  • Deque is the underlying memory continuous? no Deque is composed of a two-dimensional array. Each second dimension is continuous, and the first dimension data is not continuous.
  • The time complexity of inserting and deleting before, during and after: the middle and end are o (1), the front inserting deque is O (1), and the vector is O (n)
  • For memory efficiency: the vector is low, and the required memory space must be continuous. Deque can be stored in blocks without memory space. It must be continuous.
  • Because the deqe element is moving slowly in the middle of the two-dimensional space, the deqe element is not in the middle of the memory

What is the difference between vector and list?

  • The bottom layer of list is a two-way circular linked list
  • The addition and deletion of list is O (1), and the addition and deletion of vector is O (n)

Explain container adapter

Unordered Association container



The operator [] overload of map has two functions: one is to query, and the other is to insert a pair of data if the key does not exist

unordered_map mp1;
mp1. insert(make_pair(12,"asf"));// Generate pair type

Ordered Association container

The bottom layer is a red black tree structure.

How custom types are sorted in ordered containers: provide overloads less than operators in custom types

Iterator for container

Function object

Function object is the function pointer in C language

The object overloaded with the operator () parenthesis operator is called a function object or an imitation function.


  • Calling operator () through a function object can omit the cost of calling a function, which is more efficient than calling a function through a function pointer (inline calls are not allowed).
  • Because function objects are generated by classes, you can add relevant member variables to record more information when using function objects.
bool mygreater(T a, T b) {
    return a > b;

bool myless(T a, T b) {
    return a < b;

Class myless {// function object
    bool operator()(T a,T b){
        return a
Bool compare (t a, t B, compare COMP) {// use the function pointer or function object to call the two functions defined above
    return comp(a, b);// When using a function pointer, it cannot be declared as an inline function, which is inefficient

int main() {
    cout << compare(10, 30, mygreater)<())<

Some other uses of function objects: for priority_ Queue and set:

priority_queue,Mygreater> queue1;
priority_queue queue2;

for(int i=0;i<20;i++){

for(int i=0;i<20;i++){
    cout< set1;
set> set2;

for(int i=0;i<20;i++){

for(int a:set1){

Generic algorithms and binders

Recommended Today

Wordless Emperor

1. Eight memory elimination mechanisms 1), return the error directly, without deleting any key 2), delete the expired key 3) Randomly delete the redis key 4) Randomly delete the fast-expiring key 5), delete the redis key according to the lru algorithm 6), delete the redis key to be expired according to the lru algorithm 7), […]