Introduction to Standard Template Library (STL) in C++


We are back with our new series of articles on STL in C++. As the saying goes coders who code in C++ are certainly wild and to make them wilder, we have come-up with the dose of STL in C++ for them.

This is going to be quite long series of articles as we won’t believe in giving half knowledge to our readers.

So, if you want to excel at competitive programming then STL should be in your blood as most of the red rated coder suggest.
Be relaxed and keep reading we will make our best efforts to make you expert in STL.

  1. Firstly, let’s discuss what is STL?

    - The C++ STL (Standard Template Library) is a generic collection of class templates and algorithms that allow programmers to easily implement standard data structures like queues, lists, stacks, vector, pair, and many more...
    - In simple word STL basically have predefined containers, iterators, functions, algorithms which you can directly use in your code to make it more efficient and save your efforts to write more code.
    - Consider a example like if you want to use stack data-structure in your code then if you are not familiar with STL then you will hard-code entire class of stack and it’s functions this is where STL comes into picture, with STL you can directly make stack by just writing stack<int> myStack; voila that’s awesome isn’t it?


· The C++ STL provides programmers with the following constructs, grouped into three categories:

         a. Containers in STL

         b. Algorithms in STL

         c. Iterators in STL

We will now discuss shortly on what exactly they are.

Containers in STL:

Container library in STL provide containers that are used to create data structures like arrays, linked list, trees etc.

These containers are generic, they can hold elements of any data types, for example: vector can be used for creating dynamic arrays of char, integer, float and other types.

Containers can be divided into 3 major categories:

  1. Sequences

  • C++ Vectors

  • C++ Lists

  • C++ Double-Ended Queues

  1. Container Adapters

  • C++ Stacks

  • C++ Queues

  • C++ Priority Queues

  1. Associative Containers

  • C++ Bitsets

  • C++ Maps

  • C++ Multimaps

  • C++ Sets

  • C++ Multisets

Algorithms in STL:

STL comes with a number of algorithms that can be used directly or indirectly with our containers.

And this is really very helpful in many situations like if you want to search element in an array then you can directly use algorithm named binary_search and your task is done along with best efficiency !!

Some examples of Algorithms in STL are given below:

  1. accumulate - sum up a range of elements

  2. adjacent_difference - compute the differences between adjacent elements in a range

  3. adjacent_find - finds two items that are adjacent to each other

  4. binary_search - determine if an element exists in a certain range

Iterators in STL:

Iterators are basically pointers that point to our containers.

Iterators are used to access members of the container classes, and can be used in a similar manner to pointers.

For example, one might use an iterator to step through the elements of a vector. There are several different types of iterators:

Iterator

Description

input_iterator

Read values with forward movement. These can be incremented,compared, and dereferenced.

output_iterator

Write values with forward movement. These can be incremented and dereferenced

reverse_iterator

Either a random iterator or a bidirectional iterator that moves in reversedirection.

These were very detailed glimpse of topics which we are going to cover in this series of STL in C++ articles, so even if you are familiar with some concepts mentioned above, we will make that understanding crystal-clear in your mind with our best efforts.
If you are a newbie in STL than reading this series of articles in above mentioned sequence will make you pro in STL and you will eventually see the positive changes with your coding.

So that’s it for this article as this is just introduction to STL in C++ we will be coming up with our next article very soon till then keep learning, keep coding, keep reading and keep improving !!

References: https://www.studytonight.com/cpp/stl/stl-introduction
                   https://www.cppreference.com/Cpp_STL_ReferenceManual.pdf

Happy Coding 😊

By Programmers Army