Everything you need to know about the STL (Standard Template Library) and algorithms in C++ for Competitive Programming Part 1

Manan Aggarwal
7 min readAug 14, 2022

--

Hello, folks hope your coding life is going well. Now, this is an important blog for every competitive programmer who wants to get the offer in their dream company like google or Microsoft. I know those who are coding in C++ know the importance of STL as it plays an important role in getting jobs in our dream companies.

In this blog, I will tell you everything about STL from scratch to start your competitive programming journey to become a master in it.

Let’s get started

Guidelines for what we are discussing today

1. What is STL ?

2. Types of STL

3. Arrays

4. Iterators

What is STL?

STL are the predefined templates or data structures in C++ that can be used directly in the code for increasing the readability of the code. As it plays a huge role in competitive programming for applying all the data structures directly in the code whenever needed. Now we will discuss a little bit about the types of STL and then directly jump onto the most common STL that will be used in competitive programming.

Types of STL

There are 4 types of STL.

  1. Sequential Containers
  2. Containers Adapters
  3. Associative Containers
  4. Unordered Associative Containers

Now I will give you only a short preview that what are these containers about then we will jump to the main topic which is how to use these containers in competitive programming.

1. Sequential Containers

The containers that store the data in the container sequentially like Array, Vector, dequeue, List etc.

2. Containers Adapters

The container adapters are those containers that don’t only stores the data efficiently but also put some constraints on it during the storage and retrieval process of the elements. Example:- stack, Queue and priority_queue. We will see about each and everything in detail in the upcoming blogs.

3. Associative containers

Associative containers only store the sorted data in their containers because of these containers the data can be searched in a much more faster and efficient manner to provide you with the suitable output which you want.

Example:- Set, multiset, map and multimap.

4. Unordered Associative containers

As the name suggests the container which doesn’t have any order means the elements can be stored in random order and don’t follow the strict order to store the elements. As the elements are stored with the help of the hashing the searching can be done in O(1) time complexity best-case or O(n) time complexity worst-case.

Example:- unordered_set, unordered_map, unordered_multiset, unordered_multimap.

As I have given a brief description of the types of containers now we will start with the main topic which is how to use these containers.

3. Arrays

what are arrays ??

Arrays are the collection of items with similar datatypes which is stored in the contiguous memory location.

Now the normal declaration of the array as we all know is

datatype arr[size];
// for example
int arr[30];

This declaration will reserve the 30 slots of the memory which is 4 bytes each as an Integer takes 4 bytes each in the memory for storing values in memory. The memory is allocated during the declaration time only as it is a static declaration.

Now come to Array STL which is also declared like this and it is also a static declaration because the size is fixed.

The syntax for the Array STL

array<datatype , size> variable_name;// for example  Array<int , 30> arr;
  • If we do not insert the values in it initially then it will contain the garbage value in it.
  • If we declare the array<int, 30> arr globally then all the values will automatically be initialised by 0.

Operation Perform in the Array STL

  • arr.fill(element)

This operation is performed to fill the whole array with the given element which is passed as the function parameter in it when the array is declared locally otherwise it will take the garbage values in it.

  • arr.at(index position)

It gives the value specified at a given index position.

  • arr.front()

It gives the value which is at the first position in the array.

  • arr.back()

It gives the value which is at the last position in the array.

  • arr.size()

It gives the number of the elements which is inserted in the array at present.

This is all about the arrays Now the next topic is iterators which is the most important topic for the competitive programming.

4. Iterators

What are Iterators?

Iterators are just like pointers which point to the element stored in the memory. We can access the iterators just like pointers. Most of the STL containers have the functionality of using iterators.

There are 4 iterators that we will be using in the competitive programming is:-

  • begin()
  • end()
  • rbegin()
  • rend()

begin()

This iterator will point to the first element in the required STL container.

end()

This iterator will point to the element which is next to the last element in the container. i.e This iterator will point to the empty/garbage value in the STL container.

This will give the output as the garbage value which is not present in an array which is 32.

Now to access the last element of the array which is 100 in the given example simply subtract 1 from the iterator.

rbegin()

This iterator means the reverse begin which points to the last element stored in the container. In this, we will not subtract 1 from the arr.rbegin() to get the last element.

rend()

This iterator means the reverse end and the functionality is just the reverse of the end iterator discussed above. It points to the element which is just below the first element. Example:- if the first element at the index 0 which is always there then this iterator will point to the index -1 means to the garbage/ empty value in the container. Now to access the first element in the iterator then we have to add 1 in the arr.rend() to access the first element in the container.

reverse_end iterator (rend()) is working abnormally?

Here it is jumping to the arbitrary value which is just next to the first element so we have to perform -1 in order to get the first element in the container.

Why this is happening I will tell you in the next part of my blog

Now, this is all for Part 1 in the next part we will discuss the vectors which are dynamic array, dequeue and List. So stay tuned to learn more.

FOLLOW ME FOR FOLLOW BACK.

If you want to learn more about the VS code features then check out this blog.

Meet you with my next blog till then keep learning and keep growing.

--

--

Manan Aggarwal

I am a tech Guy who have Interest in CyberSecurity and coding and I write what I love Link to my Linkdin Profile linkedin.com/in/manan-aggarwal-76