In this article, you will be learning about STL the acronym for Standard Template Library.
should have a working knowledge of template classes.
This article is all about the Introduction to Standard Template Library (STL).
As the name suggests the Standard Template Library is a set of C++ template classes to provide common programming data structures and functions like vectors, maps, lists, sets, stacks, arrays, etc.
This library contains all the container classes, algorithms, and iterators. All the functions and methods are generalized in this library, so its components are parameterized. And that makes it easy to use the same container for any data type. For example, the sort algorithm about which we will be learning soon can sort the elements in the given container irrespective of the data type.
As STL provides numerous containers and algorithms which are very useful in competitive programming as they are easy to use and takes significantly very less lines of code for execution. If we take the implementation of a linked list it could be done quite easily with just a single statement by using the list container of the container library in STL.
STL has 4 components:
Algorithms in STL
The header algorithm defines a collection of functions specially designed to be used on the container irrespective of their type. It has complex algorithms that can be performed on different data structures.
The Algorithm library provides abstraction, i.e. you don’t necessarily need to know how the algorithm works, i.e. the internal implementation of the algorithm.
Following are the algorithms that are available in this algorithms header:
- STL Algorithms
- Array Algorithms
- Partition Algorithms
Containers in STL
The STL container library provides containers that are used to create data structures like arrays, linked lists, trees, etc. They store objects and data.
There are a total of seven standard “first-class” container classes and three container adaptor classes and only seven header files that provide access to these containers or container adaptors.
- Sequence Containers: Implements data structures that can be accessed sequentially.
- forward_list (Introduced in C++11)
- Container Adaptors: provide a different interface for sequential containers.
- priority queue
- Associative Containers: Implements sorted data structures that can be quickly searched in O (log n) complexity.
- Unordered Associative Containers: Implement unordered data structures that can be quickly searched.
Iterators in STL
Iterators in STL are used to point to the containers. They act as a bridge between containers and algorithms.
For example, the sort() algorithm has two parameters, starting iterator and an ending iterator, now sort() compares the elements pointed by each of these iterators and arranges them in sorted order, thus it does not matter what is the type of the container and the same sort() can be used on different types of containers.
The STL is one of the reasons why C++ is used for Competitive Programming.
That is all for this article we will dive deep into the containers, algorithms, and iterators in the coming article.