Let's take a quick look at the STL. STL provides three types of components: containers, iterators, and algorithms. There are two main types of containers: sequential containers and associative containers. sequential containers (vectors, lists, deque, and string, etc.) are an ordered set of elements. The associated containers (set, Multiset, map, and Multimap) include the key values for the lookup element. The function of an iterator is to traverse the container. STL algorithm library contains four kinds of algorithms: sorting algorithm, non-variable order algorithm, variable order algorithm and numerical algorithm. Here is a brief introduction to the common container in STL.
1. Vector vectors container
Vector vectors are not only random access to elements like arrays, but also can be inserted at the end of the element, is a simple, efficient container, can completely replace the array. Since the vector has the function of automatic memory management, the memory space can be adjusted dynamically for the insertion and deletion of elements, so there is no need to consider the issue of freeing space when using. When using vector vectors container, you need to include the header file "vector". (That is, # include <vector>) for the capacity definition of a vector container, a fixed size can be defined in advance and can be resized at any time afterwards (e.g. Vector<int> V (10); Defining a vector container for storing 10 elements of type int can also be used without defining its size, using the Push_back () method to extend the element from the tail, or inserting () a new element before the position of an element.
The vector container has two important methods, begin () and end (). Begin () returns an iterator to the position of the first element; end () returns an iterator that is the position of the next element of the last element. These two methods are typically used when traversing all elements of a vector. For example:
?
vector<int> v(3);vector<int>::iterator it; for(it=v.begin(); it!=v.end(); it++){...} |
C + + STL generic Programming--Application in ACM