Standard Template Library

Standard Template Library (STL) is a collection of template classes and functions ( discussed in the previous section ) that provide common data structures and algorithms. The STL is designed to provide generic and efficient implementations of common data structures (like vectors, lists, queues, and stacks) and algorithms (like sorting, searching, and manipulating sequences). It enables a C++ programmer to use these data structures without writing them from scratch. These are more optimized and save development time. That is why it is a better decision to use STL. They are mostly used in competitive programming where time is short.

Components of STL:

STL has three components.
  • Containers
  • Algorithms
  • Iterators

Containers:

Containers are data structures that store and manage collection of objects.


Containers are further:
  • Sequence containers - Array, vectors, deque etc.
  • Container Adaptors - Stacks, Queues, Priority Queues
  • Associative Containers - Set, Map, Multiset, Multimap
  • Unordered Associative - Unordered Set, Unordered Map, Unordered Multiset, Unordered Multimap

Algorithms:

Algorithms are the set of instructions ( not code ) that perform various operations on sequences of elements. Some commonly used algorithms are:

  • Sort
  • Find
  • Accumulate
  • Reverse
  • Binary Search

Iterators:

Iterators provide a way to access the elements of a container in a uniform and generic manner. They act like pointers to elements in the container and allow the traversal through the elements of container.


Types of Iterators in C++:

Input Iterator: It allows traversal in forward direction and supports read only operations. It is used in algorithms that support sequential access like finding an element in an array.
Output Iterator: It allows the assignment of values in a container and support write only operations. It is used in algorithms where modifying the elements in a container is required.
Random Access Iterator: It supports all the operations of bidirectional iterators and additional arithmetic operations like increment, decrement, and subscripting. it is used in algorithms that require direct access to elements, such as sorting algorithms.