Reference link
- https://www.bilibili.com/video/BV1et411b73Z?p=185
STL acquaintance
Birth of STL
- For a long time, the software industry has been hoping to build something that can be reused
- The object-oriented and generic programming idea of C + + aims to improve the reusability
- In most cases, data structures and algorithms fail to have a set of standards, resulting in a lot of repetitive work
- In order to establish a set of standards for data structure and algorithm, STL was born
STL basic concepts
- STL (Standard Template Library)
- STL is broadly divided into container, algorithm and iterator
- Containers and algorithms are seamlessly connected through iterators
- Almost all STL code uses template classes or template functions
STL six components
Container, algorithm, iterator, functor, adapter (adapter), space Configurator
- Container: various data structures, such as vector, list, deque, set, map, etc., are used to store data
- Algorithm: various commonly used algorithms, such as sort, find, copy and for_each et al
- Iterator: acts as the glue between the container and the algorithm.
- Imitative function: it behaves like a function and can be used as a strategy of the algorithm
- Adapter: something used to decorate a container or an interface to an emulator or iterator
- Space configurator: responsible for space configuration and management
Container, algorithm and iterator in STL
container
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:
- Sequential container: it emphasizes the sorting of values. Each element in the sequential container has a fixed position
- Associative container: a binary tree structure in which there is no strict physical order between the elements
algorithm
Algorithm: limited steps to solve logical or mathematical problems.
Algorithms are divided into: qualitative algorithm and non qualitative algorithm
- Qualitative change algorithm: refers to the content of elements in the interval will be changed during operation. For example, copy, replace, delete, etc
- 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
iterator
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; pBegin++; }
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 { public: 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 v.push_back(p1); v.push_back(p2); v.push_back(p3); //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 v.push_back(&p1); v.push_back(&p2); v.push_back(&p3); //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; } }