Standard Template Library (STL), or standard template library, influenced the architectural structure of C++ and became the core of the language. STL is a set of universal components and modern highly efficient algorithms for data management. Thanks to this C++ library, the programmer has access to advanced data structures and efficient algorithms without the need for a detailed understanding of their structure and operation.
C++ takes it to the next level
For a programmer, the STL is represented by a set 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 type of elements. In addition, the library allows you to build your own classes and algorithms that can work in conjunction with existing ones.
This approach to organizing work with data and algorithms brings C++qualitatively to another level of abstraction. Now the programmer is not burdened with the creation of dynamic arrays, lists, trees, hashes. He can also forget about programming different search and traversal algorithms. With the advent of the STL, it is enough for a programmer to define an appropriate container and use its member functions and processing algorithms.
STL components can work with arbitrary data types. This is achieved by the fact that all C++ library components are templates that allow you to use any types, as long as they are able to perform the necessary operations. That is, containers and algorithms are generalized with respect to types. This concept is called generic programming.
Despite the changes that were introduced to C++ with the advent of STL, we should not forget that the language was an efficient and multifunctional programming tool even before its appearance, and retained all its C++ features (for example, the system or ctime library) and with the advent of STL only increased.
The building blocks of a library are carefully structured components and their smooth interaction. The main such blocks are containers, iterators and algorithms. The C++ STL provides an amazing level of programming flexibility, but is also difficult to grasp and time-consuming to master.
In the C++ Standard Library, containers are used to manage collections and are made up of objects of a specific type. AllContainers have a set of pros and cons. Therefore, different containers have been developed to suit the different requirements of the programs. Containers can be arrays or linked lists. They can also be implemented with a special key for each element.
There are 3 types of containers:
- Sequential 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. There are 5 types of sequential containers: 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 itself, or the 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, the order of the elements in the collection is not affected by either the value or when the element is inserted into the collection. If you insert the nth number of elements into such a collection, their order will be unpredictable. Moreover, it can change over time. Unordered containers are: unordered set, unordered multiset, unordered map, unordered multimap.
These are the mechanisms that are used to iterate over the elements in a collection of objects. At the same time, collections can be both containers and their subset. The main advantage of iterators is that theycreate a minimal, sufficient and generic interface for any type of container. For example, one of the tasks of iterators is to move through the elements of a collection and it does not depend on the structure of this collection, which can be anything: an array, a tree, a hash table. Iterating over elements works the same way.
The interface of iterators themselves is similar to working with pointers. For example, to get the next element by an 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 use the operation "". Thus, an iterator is like a kind of smart pointer.
The main task of algorithms is to process elements of collections. For example, search or sort, change or use the value of an element. Algorithms are implemented using iterators. This approach allows you to create an algorithm only once and extend its work to any containers thanks to a single iterator interface.
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, the programmer may specify a specific search criterion. With the advent of lambda functions, it became possible to describe any operations performed on container elements when they are traversed. Thus, the C++ function library provides a very flexible option.
Does STL conflict with OOP concepts?
In a C++ library, STL data is managedcontainer classes, and operations - custom algorithms. It turns out that the concept of the STL library separates data and operations, which is contrary to the principles of object-oriented programming, which require combining data and operations. However, there is an excuse for this. Thanks to the interaction of any algorithms with any containers through iterators, a programmer can combine any data with any operations. Thus, the contradiction with OOP is eliminated and at the same time a whole new level of flexibility is achieved.
STL is a new or improved approach to programming. The beginnings of the library appeared a long time ago. The first ideas were born in 1992-1994. And after many years of development, STL became fully part of the C ++ 11 standard. The library has extensive functionality and great flexibility, but it is also difficult to understand. Its documentation spans hundreds of web pages (such as the documentation on Microsoft's Visual C++ Library site), and its description spans 1000+ page books. The library is under active development.