The Standard Template Library (STL), or the standard template library, influenced the architectural structure of C ++ and became the core of the language. STL is a set of versatile components and modern high-performance algorithms for data management. Thanks to this C ++ library, advanced achievements in the field of data structures and efficient algorithms became available to the programmer without the need for a detailed understanding of their structure and work.
C ++ goes to the next level
For a programmer, STL is represented by a multitude of collection classes designed for specific purposes, and a set of algorithms that can work with them. Due to the fact that all library components are templates, they can be used for any types of elements. In addition, the library allows you to build your own classes and algorithms that can work together with existing ones.
This approach to organizing work with data and algorithms brings C ++ to a different level of abstraction. Now the programmer is not burdened with creating dynamic arrays, lists, trees, hashes. He may also forget about programming various search and crawl algorithms. With the advent of STL, it is enough for the programmer to determine the appropriate container and use its member functions and processing algorithms.
STL components are capable of handling arbitrary data types. This is achieved by the fact that all components of the C ++ library are templates that allow you to use any types if they are able to perform the necessary operations. That is, containers and algorithms are generalized to types. This concept is called generalized programming.
Despite the changes that were introduced in C ++ with the advent of STL, one should not forget that the language was an effective and multifunctional programming tool before it appeared, and C ++ retained all its features (for example, the system or ctime library) and with the advent of STL only multiplied.
Library components
The building blocks of the library are carefully structured components and their smooth interaction. The main blocks are containers, iterators, and algorithms. C ++ STL library provides an amazing level of flexibility in programming, but at the same time it is difficult to comprehend and time-consuming to master.
Containers
In the standard C ++ library, containers are used to manage collections and consist of objects of a certain type. All containers have a set of pros and cons. Therefore, different containers have been developed to suit the various requirements of the programs. Containers can be arrays or linked lists. They can also be implemented using a special key for each element.
There are 3 types of containers :
- Serial containers. They are ordered collections. Each element has its own position, which depends on the insertion time and does not depend on the value of the element. Sequential containers of 5 varieties: array, vector, deque, list, forward list.
- Associative containers. They are also ordered collections of elements, however their position depends on the value of the element or key, if the elements of the collection are key-value pairs. There are 4 standard associative containers: set, multiset, map, multimap.
- Unordered associative containers. In this case, neither the value nor the time the element was inserted into the collection affects the order of elements in the collection. If you insert the nth number of elements into such a collection, their order will be unpredictable. Moreover, it may change over time. Unordered containers are: unordered set, unordered multiset, unordered map, unordered multimap.
Iterators
These are the mechanisms that are used to traverse elements in a collection of objects. In this case, both containers and a subset of them can be collections. The main advantage of iterators is that they create a minimal, sufficient and universal interface for any type of container. For example, one of the tasks of iterators is to move around the elements of a collection and it does not depend on the structure of this collection, which can turn out to be anything: an array, a tree, a hash table. Enumerating elements works the same way.
The interface of the iterators themselves is similar to working with pointers. For example, to get the next element by the iterator, you need to perform the “++” operation, and to get the value of the element that the iterator is currently pointing to, you need to perform the “*” operation. Thus, an iterator is like a kind of smart pointer.
Algorithms
The main task of the algorithms is the processing of collection elements. For example, search or sort, modify, or use the value of an element. Algorithms are implemented by iterators. This approach allows you to create an algorithm only once and extend its work to any containers thanks to a single interface of iterators.
For extremely complex tasks, a mechanism of auxiliary functions called by algorithms has been developed. This provides the necessary flexibility to handle specific cases. For example, a programmer may specify specific search criteria. With the advent of lambda functions, opportunities have appeared for describing any operations performed on container elements during their traversal. Thus, the C ++ function library provides very flexible options.
Does STL contradict OOP concepts?
In the C ++ library, STL data is controlled by container classes, and operations are controlled by custom algorithms. It turns out that the concept of the STL library separates data and operations, which contradicts the principles of object-oriented programming, which require the combination of data and operations. However, there is an excuse. Due to the interaction of any algorithms with any containers through iterators, the programmer can combine any data with any operations. Thus, the contradiction with OOP is eliminated and at the same time a completely new level of flexibility is achieved.
Conclusion
STL is a new or improved programming approach. The beginnings of the library appeared long ago. The first ideas arose in 1992-1994. And after many years of development, STL is fully integrated into the C ++ 11 standard. The library has extensive functionality and excellent flexibility, but at the same time is difficult to understand. Its documentation has hundreds of web pages (for example, documentation on the Microsoft Visual C ++ library website), and the description takes books over 1000+ pages. At the same time, the library is under active development.