C++ Standard Template Library ( STL )

A quick intro about C++ Standard Template Library!

Vasanth Kumar
TheLeanProgrammer
3 min readApr 29, 2021

--

What is STL in C++?

C++ Standard Template Library. Source: Taken from google.

Generally, Competitive coding has time limitations. Within that given timeframe you have to solve the given problems for which you will be rewarded some points.

Let's take an example. Let’s say there’s a question that can be easily solved using stacks. But implementing this data structure itself takes some time, right? But in C++ you can easily code a problem using stack by just including some header files and that's it, you are done!

STL in C++ stands for Standard Template Library. It has lots of inbuilt functions like sort(), rotate(), algorithms like binary search(), and pre-defined data structures like stacks, queue, linked list, forward list… Because of these time-saving pre-defined codes, programmers prefer this library.

Here’s an overview of the C++ STL.

There are 5 types of iterators namely —

  • Input Iterator,
  • Output Iterator,
  • Forward Iterator,
  • Bi-directional Iterator, and
  • Random Access Iterator

Each data structure has its own iterator. For example, a vector (similar to an array where the size of the array need not be constant in C++) has a random access iterator which means we can iterate elements in any direction whereas a forward list in CPP STL has a forward iterator which means we can iterate elements only in the forward direction.

There are 4 types of containers namely —

  • Sequence Container (Vector, Dequeue, List (Doubly LinkedList), and Forward List (Singly LinkedList) comes under this container),
  • Adaptive Container (Stack, Queue, Priority Queue comes under this container),
  • Associative Container (set, multiset, map, and multimap are under this container),
  • Unordered Associative Container ( unordered set, unordered multiset, unordered map, and unordered multiset comes under this container ).

There are plenty of algorithms in the STL library and many functions for each container. So it's always good to learn STL in C++ before starting CP as it saves a lot of time during a contest.

Also, don’t forget to implement these data structures by yourself and learn how these data structures work.

What is Competitive Programming ( a.k.a CP )?

The name “Competitive Programming” is self-explanatory. As the name suggests it’s nothing but programming but in a competitive environment. Programmers from various backgrounds participate in a contest to solve problems.

Nowadays people are taking CP as a career and teaching it to others. There are various platforms for CP such as hackerrank, HackerEarth, CodeChef, code forces ( top programmers practice here), etc. Many people got their dream job only because of CP.

Here’s the link to my GitHub repository where I made notes on C++ STL with their respective time complexity for better code optimization — https://github.com/vasanthgk02/CPP-STL-Notes

Thanks for reading this article!

Don’t forget to follow The Lean Programmer Publication for more such articles, and subscribe to our newsletter tinyletter.com/TheLeanProgrammer

--

--