Home >>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:
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.
Containers are generally of three types that are depicted below:
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:
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 |
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++.
There are mainly two functions in the iterators:
There are mainly five categories in which the iterators are divided:
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.
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.
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.
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.
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.
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:
STL Algorithms are generally categorized in five major parts that are depicted below:
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.
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; }
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 |