The standard Template Library, a set of template classes and functions, provides the programmer with an algorithm for storing information in a container that accesses the contents of an iterator that stores information.
In simple terms, it is divided into three parts, algorithms, iterators, containers, where the iterator is the bridge between the algorithm and the container, allowing programmers to operate the container through the algorithm, and these algorithms are not container-related, such as reverse algorithm, for multiple containers can be used. This article mainly introduces the standard template library (excluding the C++11 standard), if you want to learn the system, or need to see related books. STL Container
A container is an STL class for storing data, which can be divided into 2 types: Sequential container-associated containers
Sequential containers: means that the data in the container is stored in order. Sequential containers are inserted faster, but are slower to find.
Sequential containers are: vector,deque,list
Associative containers: The data in a container is stored in a key-value pair, or in a specified order, as in a dictionary, the associated container is characterized by a slow insertion speed but fast reading.
Associated containers are: Set,map,multiset,multimap
Some STL implementations also support Hash_set,hash_multiset,hash_map,hash_multimap. These containers have better search performance, regardless of the time required for the search and the size of the container. However, these containers are not a standard container, and if you want the application to be portable between different platforms, you need to follow a standard container.
A string is very similar to a container, but because string cannot access arbitrary data types, it cannot be called a container strictly. Select the correct container
Container |
type |
Advantages |
Disadvantage |
Std::vector |
Sequential container |
Insert data at end quickly Can be accessed like an array. |
Performance will be affected when resizing The search time is proportional to the size of the container Data can only be inserted at the end |
std::d eque |
Sequential container |
Have all the advantages of vector You can also insert data in the header The insertion time is also fixed |
Have all the disadvantages of vector Unlike vectors, however, deque does not need to support the reverse () function according to the specification. |
Std::list |
Sequential container |
In the middle of the list, the beginning, the end of the insert data, the time required is fixed The time required to remove the element from any location is also fixed After inserting or deleting an element, the iterator pointing to other elements is still valid |
cannot be accessed as an array with subscript Search time is slower than vector, because the list is not stored in a contiguous memory space The search time is proportional to the number of elements in the list container |
Std::set |
Associative container |
Search times are much faster than sequential containers |
Insertion time is slower than sequential container |
Std::multiset |
Associative container |
The advantages are similar to set When you need to store duplicate elements, you can use the Multiset |
The disadvantage is similar to set |
Std::map |
Associative container |
A container for accessing key-value pairs, sorted by key (map cannot be sorted by value) Search speed is not proportional to the number of elements |
|
Set::multimap |
Associative container |
You can store the same key elements The advantages are similar to the map |
The disadvantage is similar to map |
The Detail:map container is stored as a key-value pair, can only be sorted by key, if you want to sort the value, there are several solutions, 1, a new map container, the original map of the key value to the replacement order stored in the new map container, of course, to ensure that after the reverse of the map key values are unique, Otherwise, you can use Multimap. 2, create a new set or vector container, save each key value pair in the map to the new set and vector, and then sort the custom sorted function objects. STL Iterator
The simplest iterator is a pointer, given a pointer to the first element of the array, which can be incremented to access the array element in turn, and, of course, the element to which the pointer points.