Standard C++ Libraries

Table of contents:

Standard C++ Libraries
Standard C++ Libraries

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

C++ new level
C++ new 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.

Library Components

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.


STL types of containers
STL types of containers

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.


C++ Iterators
C++ Iterators

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.


Algorithm scheme
Algorithm scheme

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.

Interaction of STL Components
Interaction of STL Components


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.

Popular topic

Editor's choice

  • How to block a person in "Contact"? Protect your page from ill-wishers
    How to block a person in "Contact"? Protect your page from ill-wishers

    The popularity of social networks is increasing every day. This is due to the fact that such an instrument of mass communication has entered everyday life and firmly established itself in it, as one of the most convenient ways of communication. Almost every advanced person has his own page on the social network, where he posts his photos, posts posts and, most importantly, communicates with friends and makes new acquaintances

  • Why doesn't "VK" work? The VKontakte website is not working: what to do?
    Why doesn't "VK" work? The VKontakte website is not working: what to do?

    Social media addiction is considered one of the most serious cyber diseases. Every day, tens of millions of users around the globe visit their personal accounts, chatting with friends, discussing the latest events in the world. Social networks have become such a dense part of our lives that it seems unusual to spend a day without looking at them even for a second

  • What is button accordion? Learn slang online
    What is button accordion? Learn slang online

    The Internet is a territory with its own rules, language, style of communication, and, more recently, laws. It so happened that many new users may be left at a loss after communicating with hardened Internet - "hacks". We are talking, first of all, about the use of slang by the latter in their language, which is far from accessible to everyone who opened the page of any forum for the first time

  • Hashtag - what is it? How to use hashtags
    Hashtag - what is it? How to use hashtags

    The Internet is constantly progressing, and every day new features appear in it that make working on it more convenient. They are associated, as a rule, with the optimization and structuring of information, since there is such a large amount of it on the World Wide Web that it is sometimes difficult to find the necessary material. For example, in the recent past, so-called hashtags appeared

  • Why is the internet slow? The most important reasons
    Why is the internet slow? The most important reasons

    In the last couple of years, the Internet has become an important part of the life of almost every person. Using the World Wide Web, we can find out the weather, the latest world news, download the necessary programs, play online games, etc. When the Internet is working well, doing the above things is very nice, but what if all of a sudden web pages start to load for a very long time, and online games slow down?