Introduction to C + + STL

Posted by adriaan on Sun, 23 Jan 2022 02:21:47 +0100

Reference link


STL acquaintance

Birth of STL

  1. For a long time, the software industry has been hoping to build something that can be reused
  2. The object-oriented and generic programming idea of C + + aims to improve the reusability
  3. In most cases, data structures and algorithms fail to have a set of standards, resulting in a lot of repetitive work
  4. In order to establish a set of standards for data structure and algorithm, STL was born

STL basic concepts

  1. STL (Standard Template Library)
  2. STL is broadly divided into container, algorithm and iterator
  3. Containers and algorithms are seamlessly connected through iterators
  4. Almost all STL code uses template classes or template functions

STL six components

Container, algorithm, iterator, functor, adapter (adapter), space Configurator

  1. Container: various data structures, such as vector, list, deque, set, map, etc., are used to store data
  2. Algorithm: various commonly used algorithms, such as sort, find, copy and for_each et al
  3. Iterator: acts as the glue between the container and the algorithm.
  4. Imitative function: it behaves like a function and can be used as a strategy of the algorithm
  5. Adapter: something used to decorate a container or an interface to an emulator or iterator
  6. Space configurator: responsible for space configuration and management

Container, algorithm and iterator in STL


STL container is to implement some of the most widely used data structures

Common data structures: array, linked list, tree, stack, queue, set, mapping table, etc

These containers are divided into sequential containers and associated containers:

  1. Sequential container: it emphasizes the sorting of values. Each element in the sequential container has a fixed position
  2. Associative container: a binary tree structure in which there is no strict physical order between the elements


Algorithm: limited steps to solve logical or mathematical problems.

Algorithms are divided into: qualitative algorithm and non qualitative algorithm

  1. Qualitative change algorithm: refers to the content of elements in the interval will be changed during operation. For example, copy, replace, delete, etc
  2. Non qualitative algorithm: it means that the element content in the interval will not be changed during the operation, such as finding, counting, traversing, finding extreme values, etc


A method is provided to search the elements contained in a container in order without exposing the internal representation of the container

Each container has its own iterator

Iterators use much like pointers

Iterator type:

The commonly used iterators in containers are bidirectional iterators and random access iterators

Initial knowledge of container algorithm iterator

The most commonly used container for STL is Vector, which can be understood as an array.

vector stores built-in data types

Containers: vector
Algorithm: for_each
Iterator: vector < int >:: iterator

The first traversal method:

vector<int>::iterator pBegin = v.begin();
vector<int>::iterator pEnd = v.end();

while (pBegin != pEnd)
    cout << *pBegin << endl;

The second traversal method:

for (vector<int>::iterator it = v.begin(); it != v.end(); it++)
    cout << *it << endl;
cout << endl;

The third traversal method:

void MyPrint(int val)
    cout << val << endl;

for_each(v.begin(), v.end(), MyPrint);

Vector stores custom data types

class Person
    Person(string name, int age)
        this->m_Name = name;
        this->m_Age = age;
    string m_Name;
    int m_Age;

void test()
    vector<Person> v;
    Person p1("aaa", 10);
    Person p2("bbb", 20);
    Person p3("ccc", 30);

    //Add data to container

    //Traverse the data in the container
    for(vector<Person>::iterator it = v.begin(); it != v.end(); it++)
        //Cout < < name: < < (* it) m_ Name < < age: < < (* it) m_ Age << endl;
        cout << "full name:" << it->m_Name << " Age:" << it->m_Age << endl;

Vector stores custom data type pointers

void test2()
    vector<Person*> v;
    Person p1("aaa", 10);
    Person p2("bbb", 20);
    Person p3("ccc", 30);

    //Add data to container

    //Traverse the data in the container
    for(vector<Person *>::iterator it = v.begin(); it != v.end(); it++)
        cout << "full name:" << (*it)->m_Name << " Age:" << (*it)->m_Age << endl;

Topics: C++ data structure Container vector