Standard Template Library (STL): A Brief Introduction

A Complete Overview Of Standard Template Library (STL): 

Standard Template Library (STL) of C++ is a collection of template classes that provide data structures such as arrays, vectors, queue, etc. STL is a library consisting of containers, algorithms, and iterators.

As STL consists of a collection of template classes, it’s a generalized library that is independent of data types.

=> Read Through The Extensive C++ Training Tutorial Series Here.

Standard Template Library (STL)

Components Of STL

STL mainly consists of the following components which are mentioned below:

#1) Containers

A container is a collection of objects of a particular type of data structure. In STL, we have various types of container classes like Array, vector, queue, deque, list, map, set, etc. These containers are generic in nature and are implemented as class templates.

Containers are dynamic in nature and can be used to hold various types of objects.

#2) Algorithms

Algorithms are the methods or functions that act on containers. By using algorithms provided by STL, we can have methods to search, sort, modify, transform or initialize the contents of container class objects.

Algorithms provided by STL have built-in functions that can directly operate on complex data structure instead of having to write the algorithms ourselves.

For Example, reverse() function in STL can be used to reverse the linked list.

#3) Iterators

Iterators are the very important and distinguishing feature of STL. Iterators are the constructs that are used to traverse through the container objects. Similar to indexes that we use to step through the arrays, Iterators act on container class objects and can be used to step through the data.

Containers

Containers store objects and data. They are basically template-based generic classes.

Containers in STL are divided into the following types:

#1) Sequential Containers

Containers the can be accessed in a sequential or linear manner are said to be sequential containers.

Arrays, Vectors, Lists, Deques are the STL containers that store data linearly and can be accessed in a sequential manner.

#2) Associative Containers

Associative containers are containers that implement sorted data structures. These containers are fast to search. Some of the Examples of associative containers are Map, Set, MultiMap, Multiset, etc. These containers are usually implemented in a key/value pair fashion.

#3) Container Adopters

Container adopters are sequential containers, however, they are implemented by providing a different interface. Thus containers like a queue, deque, stack, and priority-queue are all classified as container adopters.

Iterators

Iterators are constructs that we use to traverse or step through containers in STL. Iterators are very important in STL as they act as a bridge between algorithms and containers. Iterators always point to containers and in fact algorithms actually, operate on iterators and never directly on containers.

Iterators are of following types:

  • Input Iterators: Simplest and is used mostly in single-pass algorithms.
  • Output Iterators: Same as input iterators but not used for traversing.
  • Bidirectional Iterators: These iterators can move in both directions.
  • Forward Iterators: Can be used only in the forward direction, one step at a time.
  • Random Access Iterators: Same as pointers. Can be used to access any element randomly.

Algorithms

Algorithms are a set of functions or methods provided by STL that act on containers. These are built-in functions and can be used directly with the STL containers and iterators instead of writing our own algorithms.

STL supports the following types of algorithms:

  • Searching algorithms
  • Sorting algorithms
  • Modifying or manipulating algorithms
  • Non-modifying algorithms
  • Numeric algorithms
  • Min/Max algorithms

As each of the algorithm types suggests, these algorithms can be used to achieve different functionality in STL containers like searching, sorting, transforming the data in the containers, finding min/max value, etc.

Conclusion

This is the brief introduction of Standard Template Library. In our upcoming tutorials, we will learn more about each of the containers, algorithms, and iterators.

=> Check Complete C++ FREE Training Series Here.