The three most classic containers in C + + are Vector, Map and List

There are many kinds of containers in C + +. The common ones are sequential containers: vector, deque and list. There are also associated containers: set, multiset, map and multimap. There are too many containers. Let's talk about the three most commonly used containers: vector, list and map.

First: Vector container

Sketch Map:

characteristic:

The address remains unchanged and the memory space is continuous, so inserting and deleting in the middle will cause memory block copy. If you insert a structure or class, it will cause construction and destruction. The performance is not particularly high, and the operation on the end element is the fastest.

Here are some functions commonly used by vector containers:

Example:

#include <iostream>
#include<vector>

using namespace std;

int main()
{
    //Define a vector container a and insert 10 numbers into container a
    vector<int> a;
    for(int i=0;i<10;i++)
    {
        a.push_back(i);
    }
    //Define an iterator
    vector<int>::iterator it;
    //Vector containers can be accessed either through subscripts or iterators
    cout<<"Access via subscript:"<<endl;
    for(int i=0;i<a.size();i++)
    {
        cout<<a[i]<<" ";
    }
    cout<<endl;
    cout<<"Access via iterator"<<endl;
    for(it=a.begin();it!=a.end();it++)
    {
        cout<<*it<<" ";
    }

    return 0;
}

Advantages: it supports random access, that is, subscript access and iterator access, so the query efficiency is high.

Disadvantages: when inserting or deleting elements into the head or middle, in order to maintain the original relative order, all elements after insertion or deletion must be moved, so the insertion efficiency is relatively low.

Applicable scenario: it is applicable to scenes with simple objects, small changes and frequent random access.

Second: List container

Sketch Map:

characteristic:

List is a bidirectional linked list. Elements are stored in the heap. Each element is placed in a piece of memory. Its memory space can be discontinuous. Data can be accessed through pointers. There is no iterator provided. Each element deleted will release the memory it occupies. It can be inserted and deleted anywhere. The first and last elements are accessed the fastest, and the access time of other elements is the same.

Here are some functions commonly used in the List container:

Example:

#include <iostream>
#include<list>
using namespace std;

int main()
{
    list<int> a;
    list<int> b;
    for(int i=0;i<10;i++)
    {
        //Insert after
        a.push_back(i);
        //Front insert
        b.push_front(i);
    }
    cout<<"This is the list a"<<endl;
    while (!a.empty())
    {
        cout<<a.front()<<" ";//Access the first element of the list
        a.pop_front();//Delete this element after accessing
    }
    cout<<endl;
    cout<<"This is the list b"<<endl;
    while (!b.empty())
    {
        cout<<b.front()<<" ";//Access the first element of the list
        b.pop_front();//Delete this element after accessing
    }
    return 0;
}

Advantages: discontinuous memory, dynamic operation, can be inserted or deleted at any position, and high efficiency.

Disadvantages: random access is not supported

Applicable scenarios: scenes that are frequently inserted and deleted and are not frequently accessed randomly.

The third type: Map container

Map is implemented by a red black tree, and its elements are "key value / real value", forming a key / value Paris. Each element has a key, which is the basis of sorting criteria. Each key can only appear once and cannot be repeated.

Map is mainly used for one-to-one mapping of data. A red black tree is built in map. This tree has the function of automatic sorting of data, so all data in map is orderly. For example, in a class, there is a one-to-one mapping relationship between each student's student number and his name.

Here are some common containers for Map containers:

Example:

#include <iostream>
#include<map>
using namespace std;

int main()
{
    map<int,string> Student;
    Student.insert(pair<int,string>(1,"student1"));
    Student.insert(pair<int,string>(2,"student2"));
    Student.insert(pair<int,string>(3,"student3"));
    map<int,string>::iterator it;
    for(it=Student.begin();it!=Student.end();it++)
    {
        cout<<it->first<<" "<<it->second<<endl;

    }

    return 0;
}

Advantages: it is realized by using balanced binary tree, which is easy to find elements, and can map one value to another.

Disadvantages: the red black tree needs to be adjusted for each insertion, which has a certain impact on the efficiency.

Keywords: C++ Algorithm Interview STL

Added by j0hn_ on Sat, 18 Dec 2021 05:23:23 +0200