STL containers. Part 2

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

STL It introduces a number of concepts and data structures, that in almost all cases, allow greatly simplify code. We introduce the following concepts category:

  1. Container - Storage of a set of objects in memory.

  2. Iterator - means of access to the contents of the individual objects in the container.

  3. Algorithm - Identification of the most standard computational procedures on containers.

  4. Adapter - Adaptation of the main categories for the most commonly used interfaces (such as a stack or queue).

  5. Functor (functional entity) - Concealment function in an object to use it in other categories.

Library STL - This is a very vast area. Her description of the book are devoted to the whole individual (One of the best books in Russian, sufficient for the development of STL in detail, It is shown at the end of the text). We are, because of the initial level of love, consider STL technique to intuitively clear examples.

Syntax STL It based on the use of syntax of C ++ as templates (templates) classes and function templates. But for the successful application of technology STL not necessarily a deep understanding of technology templates.

The central concept STL, around which everything else revolves, this is container (More use of the term collection). Container - a collection of a number of similar elements, packaged in a container in a certain way. Just a prototype container to the classical language is C ++ array. the way, which elements are packed in the container, It determines the type of container and behavior of elements in a container. STL It introduces a number of different types of containers:

  • successive containers - vector (vector), doubly linked list (list), DEK (and);

  • associative containers - sets (set and multiset ), hashtable (map and multimap);

  • pseudo-containers - bitmaps (bitset), strings (string and wstring), arrays (valarray);

We consistently look at the examples of the use of the main container, moving from more simple to more complex. The simplest type of them is vector. Closest prototype vector is an array of C ++. But the size of the vector at any time can change dynamically adding operations (method push_back()) or delete an item. Same, as array, we can turn to an arbitrary element vector indexing operation [n]. This is is the first, the surface layer of knowledge about vector, which allows us to start working with him:

Description vector<float> (this is the previously mentioned template the class description) declares in the code an object array: vector elements of type float.

Next we see such class methods vector<float>, how max_size() - The maximum possible length of the vectors in general (implementation of a constant); size() - The current size (the number of elements) vector; capacity() - The current capacity of the vector (the maximum number of elements, which can be placed in a vector placing it in the current). Doing this fragment gives something like the following (details can vary depending on the implementation):

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

Here you can see quite interesting vector behavior (in this sense and it): when adding the next element, capacity vector is not enough, done new vector accommodation, twice the capacity of reserving for him (with a margin, to include the addition of a new element is not required once the new reallocation).

So we got the equivalent of an array C ++, which size (size()) changes in arbitrary units ranging from a few to millions of elements. Pay attention (it is very important), what increasing the size of the vector is achieved not indexation beyond its current size, and "huddle" (method push_back()) new element in the end vector. Another way to change the size of the vector - is to call methods resize(). That is why, provided for the vector 2 different method of indexing: operation [n] and a method function at(n). Method at() checks the current size of the vector size(), and indexing works for his border an exception (this is mistake!). In front of, indexing operation is not border checks, is unsafe, but it is faster.

containers STL C ++ , Standard Template Library, c ++ container[1] Scott Meyers, Effective use of STL, "Peter" St. Petersburg. 2002, ISBN: 5-94723-382-7, 244 str.. The link can be read online book.

Newsletter of programming:

STL containers. Part 2
5 (100%) 2 votes

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.

Leave a Reply

Your email address will not be published. Required fields are marked *