Home >>C++ Standard Template Library Tutorial(STL)

C++ Standard Template Library Tutorial(STL)

The C++ Standard Template Library Tutorial(STL)

The set of C++ template classes that are known to deliver common programming data structures and functions like lists, arrays, stacks, etc. is known as the Standard Template Library (STL). This is a library of the container classes,

Algorithms, and iterators and the contents of it are paramaterized as it is treated as a generalized library. In order to work with the Standard Template Library (STL) the programmers should have a working knowledge of the template classes .

Here are the four components of the STL:

  • Containers
  • Iterators
  • Algorithms
  • Functions

1. Containers

The objects that generally hold the data of the same type are known as the containers in the C++ programming. In order to implement different data structures like arrays, trees etc., the containers are used.

Classification of Containers

Containers are generally of three types that are depicted below:

  • Sequence containers
  • Associative containers
  • Derived containers

Please note : Each container class generally consists a set of functions that can be used to manipulate the contents.

Here is the list of the containers that is depicting details of all the containers as well as the header file and the type of iterator that is associated with them:

List of the containers

Container Description Header file iterator
vector Vector is generally a class in C++ that is used to create a dynamic array that allow the insertions and deletions at the back. <vector> Random access
list This is the sequence containers that generally allow the insertions and deletions from anywhere. <list> Bidirectional
deque The double ended queue that generally allows the insertion and deletion from both the ends is known as deque. <deque> Random access
set An associate container that is generally used for storing unique sets is known as a set. <set> Bidirectional
multiset An associate container that are generally used for storing non- unique sets are known as multiset. <set> Bidirectional
map An associate container that is used for storing unique key-value pairs is known as Map, in other words, each key is generally associated with only one value (one to one mapping). <map> Bidirectional
multimap An associate container that is used for storing key- value pair, and each key that can be associated with more than one value is known as multimap. <map> Bidirectional
stack Stack generally follows the last in first out(LIFO). <stack> No iterator
queue Queue generally follows first in first out(FIFO). <queue> No iterator
Priority-queue The first element that is out is always of the highest priority element. <queue> No iterator

2. Iterator

Iterators are pointer-like entities that are used to access the individual elements in a container in C++ are known as the iterators.

The iterators are known to move sequentially from one element to another element and this process is generally known as iterating through a container in C++.

Functions in Iterators

There are mainly two functions in the iterators:

  • begin() : An iterator to the first element of the vector is generally returned by the member function begin().
  • end() : An iterator to the past-the-last element of a container is generally returned by the member function end().

Iterator Categories

There are mainly five categories in which the iterators are divided:

  • Input iterator
  • Output iterator
  • Forward iterator
  • Bidirectional iterator
  • Random Access Iterator

a. Input iterator

An iterator that generally allows the program to read the values that are within the container is known as input iterator. Just by dereferencing the input iterator that allows us to read the value from the container and the catch is that it does not alter the value. An Input iterator in C++ is generally a one way iterator. An Input iterator in C++ can only be incremented but users or programmers cannot decremented it.

b. Output iterator

An output iterator in C++ is very similar to the input iterator except the fact that it allows modification of the value of the container by the program but the program is not allowed to read it. An output iterator is generally known as a one-way iterator and is only a write iterator.

c. Forward iterator

Forward iterator in C++ is known to use the ++ operator in order to navigate through the container. Forward iterator generally passes through each and every element of a container and the sequence is of one element at a time.

d. Bidirectional iterator

A Bidirectional iterator in C++ is generally known to be very similar to the forward iterator apart from the fact that it also moves in the backward direction. A Bidirectional iterator is known to be a two way iterator and can be incremented and decremented as well.

e.Random Access Iterator

Random access iterator in C++ programming is generally used in order to access the random element of a container. Random access iterator possesses all the features that are possessed by a bidirectional iterator including one more additional feature, i.e., pointer addition. By using the, the random element of a container can be accessed just by the use of the pointer addition operation.

3. Algorithms

The functions that are used across a variety of containers in order to process its contents is known as the Algorithms in STL.

Here are some of the important points that should be remembered while using the pointers:

  • Algorithms in C++ are known to deliver approx 60 algorithm functions that are used to perform the complex operations.
  • Standard algorithms in C++ generally permit the programmers to work with two different types of the containers simultaneously.
  • The important point that should be kept in mind is that the algorithms are not the member functions of a container instead they are known to be the standalone template functions in STL.
  • These algorithms are known for their time saving property and effort.
  • The <algorithm> header file must be included in the program, in order to access the STL algorithms.

Categorization of the STL Algorithms

STL Algorithms are generally categorized in five major parts that are depicted below:

  • Nonmutating algorithms : The algorithms that are known to not alter with any value of a container object and they have nothing to do with the change in order of the elements in which they appear are known as the Nonmutating algorithms . These algorithms are used for all the container objects and the real use of the forward iterators is made by them only.
  • Mutating algorithms : The algorithms that can be generally used to alter the value of a container and have the authority to change the order of the elements in which they appear are known as the Mutating algorithms.
  • Sorting algorithms : The modifying algorithms that are used to sort the elements in a container are known as the Sorting algorithms in STL.
  • Set algorithms : The algorithm that is generally used to execute some of the functions on a container that results in improving the efficiency of a program is known as the Set algorithms. Another name by which this algorithm is known is sorted range algorithm.
  • Relational algorithms : The algorithms that are generally used to work on the numerical data are known as the Relational algorithms. These algorithms are designed mainly to execute the mathematical operations over all the elements that are present in a container.

4. Function Objects

A function that is basically wrapped in a class in order to make it look like an object is known as the Function object. The characteristics of a regular function are extended by the function object just by using the feature of an object oriented just like generic programming. Hence, it can be said that the function object is basically a smart pointer that are known to have multiple advantages over the normal function in STL.

Some of the advantages of function objects over a regular function in STL depicted below:

  • Function objects are known to have the member functions as well as member attributes in them.
  • The function objects can be initialized before their usage in STL.
  • Regular functions are known to have various types only in the condition when the signature differs and the function objects in STL can also possess various types even when the condition is that the signatures are also the same.
  • These function objects are also known to be faster than the regular functions in STL.

A function object is generally known by a different name that is 'functor'. An object that consists of at least one definition of the operator() function in STL is called as the function object. And that basically means that if in case the programmer declare the object 'P' of a class in the operator() function in which it is defined and can be able to use the object 'P' just like a regular function in STL.

Let's consider that 'P' is an object of a class and then the operator () function in STL can be called as the method that is depicted below:

P(); 

which is same as:

P.operator() ( ); 

Here is an example of the function object that will help you understand the same in greater depth and will make you understand the use of the function object in the STL:

#include <iostream>  
 using namespace std;  
     class func_object  
     {  
         public:  
        int operator()(int x, int y)              
       {  
           return x+y;  
       }  
    };  
  
     int main()  
    {  
       func_object fun;                 
       int res = fun(10,5);  
      cout<<"Sum of x and y is : "<<res;  
    return 0;  
}  
Output :Sum of x and y is : 15

C++ Standard Template Library Tutorial(STL) Index

Sr.No. Topics
1 C++ Vector
2 C++ Deque
3 C++ List
4 C++ Set
5 C++ Stack
6 C++ Queue
7 C++ Map
8 C++ Algorithm
9 C++ Iterators
10 C++ Bidirectional iterator
11 C++ Forward Iterator
12 C++ Input Iterator
13 C++ Output Iterator