C++ STL Components
In today’s article, we are going to learn about all the points things that is related to STL in C++ so stay connected because you are going to learn many things in simplest way possible.
Let’s first understand the full form of the STL in C++. So STL stands for Standard template library.
Standard template library (STL) in C++ is the library which provides us the set of C++ template classes which further provides us the generic classes and functions that we are using in implementation of data structures and algorithms.
Components of STL
So basically STL contains four components and those are given below:
- Function object.
These four components are further described properly.
Containers are a one way of organizing data in memory. In other words, we can also say that a container holds the data which is of same type.
Example – arrays, tree, list etc. we can implement these data structures using containers.
Containers are further divided into three classifications and those are:-
- Sequence containers
- Associative containers
- Derived containers
- Un-ordered Associative containers.
The sequence container is used to implement Data structure which need to be accessed in a sequential manner. Examples would be:- Vector, List, deque.
Associative container is used to implement the stored data structure. Example would be- Multiset, Set, Map, multimap.
Un-ordered Associative containers:-
Un-ordered Associative containers are used to implement un-stored data structure. Example would be- Unordered -Multiset
Derived containers are used to provide different interface to the sequence containers in C++. Example would be- Queue
Here are following containers with description, header file and iterator.
|Vector||Vector is also known as class in C++ STL which allows insertion and deletion at the back.||<vector>||Random access|
|Deque||Deque us basically a double ended queue which allows us the insertion and deletion at both side.||<deque>||Random access|
|List||We can also refer to list as a sequence container which allows us the insertion and deletion from anywhere.||<list>||Bidirectional|
|Set||We can also refer to set as a associate container for storing the unique sets.||<set>||Bidirectional.|
|Multiset||Multiset is also a associate container for storing the non-unique sets.||<sets>||Bidirectional|
|Stack||Stack follows last in and first out (LIFO)||<stack>||No iterator|
|Queue||Queue follows first in and first out (FIFO)||<queue>||No iterator|
|Priority_queue||In Priority_queue, first element is the highest priority element always.||<queue>||No iterator|
|Map||Map is also an associate container to store unique key- value pair.||<map>||Bidirectional|
|Multimap||It is also associate container for storing key value pair, and each key can associate with more than one value||<map>||Bidirectioanl|
When we need to access the individual element in the container then we use iterator. Because iterator is referred as the pointer-like entities. When iterator is moving sequentially from one element to other element then this process is also known as iterating through containers.
Only two functions contain by iterator. Those are begin and end.
Begin():a member function begin() return the iterator to the first element of the vector.
End(): a member function end() returns the iterator to past the last element of that container.
Standard template library (STL) in C++ provides us many algorithms that can be used with any of the container and the type of container will not affect the operation.
Algorithms are the library which contains built in function that can perform complex algorithms on the data-structure.
Example of some of the algorithms which are present in the algorithm library:
- Sort() = Which input or range we are giving, this algorithm helps in sorting. In either ascending order or descending order.
- Reverse() = This function reverses the range.
In algorithm, there are approximately 60 algorithms (function), and these functions help in performing complex operations.
We are also allowed to work with two different algorithms at a same time.
These algorithms help in saving efforts and time. To trigger these algorithms, we need to include <algorithm> header file in the program first.
Types of algorithms in Standard template library (STL) in C++ areas follow:
- Sorting Algorithm.
- Mutating Algorithm.
- Non-Mutating Algorithm.
- Set Algorithm.
- Relational Algorithm.
These algorithms are further described briefly.
- Sorting Algorithm: In Standard template library (STL), sorting algorithm helps in modifying the algorithms which is used to sort the elements in the container.
- Mutating Algorithm: In Standard template library (STL), mutating algorithm is used to change the order of any element in which it is representing by altering the value of the container.
- Non-Mutating Algorithm: In Standard template library (STL), Non- Mutating Algorithm means that opposite of mutating algorithm which means non mutating algorithm does not change the order of any of the element which is present in the container nor it alter any value of the container.
- Set Algorithm: In Standard template library (STL), Set Algorithm helps to perform some functions on the container which improves the efficiency of the program that is why this algorithm is also well-known as sorted range algorithm.
- Relational Algorithm: In Standard template library (STL), Relational Algorithm are basically those algorithms which perform all the mathematical operation on the elements which is present in the container so in other words we can also say that Relational Algorithm is used to perform operation on numerical data.
Basically, iterators are used to point at the memory address of the Standard template library (STL) containers, and they are primarily used in sequence of int, char etc.
In other words, we can also refer to iterators of Standard template library (STL) as whenever we need to access the individual element in the container, we use iterator. Because it referred as the pointer-like entities.
- We use iterators because it helps in reducing the complexity of the program.
- It also helps in executing the program in less time than iterators.
Different operations which are performed on iterators in Standard template library (STL) are as follows:
Let’s discuss about the details of these operations.
- Advance():- The advance() operation means that it will increment the iterator I by the value of the distance and if the value of the distance is negative then instead of incrementing it , it will decrement.
Syntax would be: advance(iterator I, int distance)
- Distance():- distance operation means that it will help in returning the number of elements or the distance between first and last iterator and these both are the same.
Syntax would be: distance(iterator first, iterator last)
- Next():- next() means it will return nth iterator to i, which in simple words means that iterator pointing to nth element from the element pointed by the i.
Syntax would be:- next(iterator I, int n)
- Previous():- prev() means that it will return nth predessor to i, which in simple words means that iteator pointing to nth predessor element from the element pointed by i.
Syntax would be:- prev(iterator I, int n)
This is same as the next() but the only difference here is that int n here is pointing the nth predessor.
- Begin():- begin() means it brings the iterator to the start of the given container.
Syntax would be:- begin()
- End() :- end() means it returns the iterator to the end of the given container.
Syntax would be:- end()
These are the different operations which are performed on the iterators in Standard template library (STL)