C++ STL Library

Shivani Mehta
4 min readApr 27, 2020

--

Hola Guys! Are you new to competitive programming? Do you fear reading those jargons in the c++ code of other programmers? Do the keywords or terms in the code of others sound alien to you? If you answer yes to any of the above questions — then this article is just for you.

In this article, we’ll learn about one of the most important c++ library when it comes to competitive programming, so let’s get started.

C++ STL is a set of classes to provide all the available data structures and algorithms which are already implemented in the C++ Standard library.

Note that the C++ STL and C++ standard library are two different libraries.

C++ STL enables programmers to write our code more quickly and efficiently.

Let’s break the term to get the true sense of what STL is?

Standard — This is called as standard because it’s ISO standardised. Every modern compiler should support the use of it.

Template — The template is a kind of special functions which binds its parameters at compile time. STL is a templated one. What does that mean? Let’s understand with an example.

Suppose we have a vector of integers and another one is a list of Integers.

As we all know these both containers support some generic functions like inserting/deleting/ updating or printing elements. They are many more methods but just for simplicity let’s print the elements of the given container.

Here in this way, the function can take an argument and convert the called function with the template available.

Library — Because it supports a rich set of functions and algorithm to bind it to any container. Some examples of functions available are searching, sorting etc.

Components of STL

There are majorly 4 components of C++ STL.

  1. Containers
  2. Algorithms
  3. Iterators
  4. Functions

Let’s read about them one by one-

Containers

In layman terms, Containers are nothing but data structures which contains data and provides member functions to access them, either directly or through iterators (reference objects with similar properties to pointers). Containers further are divided into 3 types:-

Sequential — Containers in which position of an element matters.For eg vector, list, deque, forward_list etc.

Associative — Containers in which the position of an element doesn’t matter. Indexing is not possible in such containers. Eg Sets, maps, unordered_maps, unordered_sets, multisets, multimaps etc.

Derived — These are a special type of containers which are pure concepts. There is no physical significance to these types of containers. For eg -Stack, queue, priority_queue etc.

To use any of the container listed above, we need to include the appropriate header file. for instance, to use the list container we can use the below statement.

#include<list>

Algorithm

An algorithm as you all must be aware of providing a way to perform various operations on the available containers. We need to include a special header file to embed the functionality in our code.

#include<algorithm>

Iterator

An iterator is any object that, pointing to some element in a range of elements, can iterate through the elements of that range for either reading or writing.

The most obvious form of the iterator is a pointer: A pointer can point to elements in an array and can iterate through them using the increment operator (++).

Notice that while a pointer is a form of iterator, not all iterators have the same functionality of pointers; Depending on the properties supported by iterators, they are classified into five different categories:

Input and output iterators are the most limited types of iterators: they can perform sequential single-pass input or output operations.

Forward iterators have all the functionality of input iterators and -if they are not constant iterators- also the functionality of output iterators, although they are limited to one direction in which to iterate through a range (forward). All standard containers support at least forward iterator types.

Bidirectional iterators are like forwarding iterators but can also be iterated through backwards.

Random-access iterators implement all the functionality of bidirectional iterators and also have the ability to access ranges non-sequentially: distant elements can be accessed directly by applying an offset value to an iterator without iterating through all the elements in between. These iterators have similar functionality to standard pointers (pointers are iterators of this category).

Functors

Functors are objects that can be treated as though they are a function or function pointer. Functors are most commonly used along with STLs in a scenario like the following:

For eg Sort() is available for almost all the sequential container which by default sorts the container in increasing order. But what if we need to sort the container the other way i.e decreasing order. In that case, we use functors to specify additional functionality to the applied method.

That’s all for this article. In the upcoming article, we’ll learn more about some specific containers that are widely used in competitive programming and it’s associated algorithms.

If you like the article or have some queries regarding this article do let me know. I would appreciate the effort.

Lastly, A clap will motivate me to write more such article. So don’t forget to press the clap button below. Thank you ‘ )

--

--

Shivani Mehta
Shivani Mehta

No responses yet