STL containers: vector. Part 3

STL containers: vector. Part 3
5 (100%) 1 vote




containers STL C ++ , Standard Template Library, vector ++, vector c++

At the previous lesson we have defined a variable of type vector<float>, as an equivalent array, the size of which we can arbitrarily change the course of the execution of code. But this does not mean (as well as vectors for classical C ++), we can thus create only dynamic arrays of simple built-in types. Type of item vector It can be arbitrary and arbitrarily complex! For example, we could describe as a student group:

containers STL C ++ , Standard Template Library, vector ++, vector c++

In this example,, inter alia, we applied this pointer and operator overloading. I leave links to you, if someone needs to refresh it in memory.

Vector template class (and any STL container) may be, in its turn, container STL. For example vector< vector<int> > or vector< vector< vector<int> > > (do not forget the space between the closing bracket ‘>‘ - It is a feature of syntactic parser). Thus we can, for example, create a class of triangular matrices:

containers STL C ++ , Standard Template Library, vector ++, vector c++
The next level of our going into vectors technique, and STL containers in general, It is the concept of iterator. Iterator - the central concept to work with STL containers. iterator – it is an abstraction, which is used to iterate (iterate) STL elements in a container and providing access to the individual elements. iterator p It is not a pointer, but, at first, you can conditionally consider it as something similar in appearance: *p will denote the value of the data under the current iterator, p the iterator to the next item in the container, and p– (when this is permissible) - In the previous item. For different types of containers, corresponding iterators may apply to one of 5 categories: inputs, weekend, unidirectional, bidirectional and random access. vectors iterators - an iterator direct access. That is why vectors can be indexing operation. these, enough surface, Knowledge about iterators us enough, to start working with them.

Reproduce in terms of iterators with the task of finding all the prime numbers, not exceeding N (sieve of Eratosthenes), we have already decided earlier Technology C ++ arrays:

As can easily be seen from the description vector<bool>::iterator, iterator that keeps a kind of container, which refers to, and the type of this container. This requires quite cumbersome to write an accurate description of the type of iterator. But the latest C ++ 11 standard introduced the concept hatchability type: if the desired object type is derived from the context of its use, the type of the object can be declared descriptor auto (output type). Then the string 16 the code shown above can be written as:

Finally, vectors (and for all containers, having bi-directional iterators, as mentioned above) It can be defined Reverse iterators, are not moved from the beginning to the end of the container, and vice versa - from the end to the beginning. This iterator must be declared as a very different type of, for example:

But here, we can rely on type inference, as in the following example:

containers STL C ++ , Standard Template Library, vector ++, vector c++

All arising questions to ask in the comments lesson.

Newsletter of programming:

Olej

About Olej

practical experience about software development 40 years. Teacher Global Logic international software company. IBM Developer Works Permanent author of publications. Scientific editor of the computer literature publishing house "Symbol-Plus", St. Petersburg.

2 thoughts on “STL containers: vector. Part 3

Leave a Reply

Place code in tags: <pre class="lang:c++ decode:true ">YOUR CODE</pre>