Containers can be described as the objects that hold the data of the same type. Containers are used to implement different data structures for example arrays, list, trees, etc.
Following are the containers that give the details of all the containers as well as the header file and the type of iterator associated with them :
Container | Description | Header file | iterator |
---|---|---|---|
vector | vector is a class that creates a dynamic array allowing insertions and deletions at the back. | <vector> | Random access |
list | list is the sequence containers that allow the insertions and deletions from anywhere. | <list> | Bidirectional |
deque | deque is the double ended queue that allows the insertion and deletion from both the ends. | <deque> | Random access |
set | set is an associate container for storing unique sets. | <set> | Bidirectional |
multiset | Multiset is an associate container for storing non- unique sets. | <set> | Bidirectional |
map | Map is an associate container for storing unique key-value pairs, i.e. each key is associated with only one value(one to one mapping). | <map> | Bidirectional |
multimap | multimap is an associate container for storing key- value pair, and each key can be associated with more than one value. | <map> | Bidirectional |
stack | It follows last in first out(LIFO). | <stack> | No iterator |
queue | It follows first in first out(FIFO). | <queue> | No iterator |
Priority-queue | First element out is always the highest priority element. | <queue> | No iterator |
Classification of containers :
begin(): The member function begin() returns an iterator to the first element of the vector.
end(): The member function end() returns an iterator to the past-the-last element of a container.
Iterators are mainly divided into five categories:
Operations supported by iterators
iterator | Element access | Read | Write | Increment operation | Comparison |
---|---|---|---|---|---|
input | -> | v = *p | ++ | ==,!= | |
output | *p = v | ++ | |||
forward | -> | v = *p | *p = v | ++ | ==,!= |
Bidirectional | -> | v = *p | *p = v | ++,-- | ==,!= |
Random access | ->,[ ] | v = *p | *p = v | ++,--,+,-,+=,--= | ==,!=,<,>,<=,>= |
Algorithms are the functions used across a variety of containers for processing its contents.
Points to Remember:
STL algorithms can be categorized as:
A Function object is a function wrapped in a class so that it looks like an object. A function object extends the characteristics of a regular function by using the feature of aN object oriented such as generic programming. Therefore, we can say that the function object is a smart pointer that has many advantages over the normal function.
Following are the advantages of function objects over a regular function:
A function object is also known as a 'functor'. A function object is an object that contains atleast one definition of operator() function. It means that if we declare the object 'd' of a class in which operator() function is defined, we can use the object 'd' as a regular function.
Suppose 'd' is an object of a class, operator() function can be called as:
d(); which is same as: d.operator() ( );
Let's see a simple example:
#include <iostream> using namespace std; class function_object { public: int operator()(int a, int b) { return a+b; } }; int main() { function_object f; int result = f(5,5); cout<<"Addition of a and b is : "<
Addition of a and b is : 10
In the above example, 'f' is an object of a function_object class which contains the definition of operator() function. Therefore, 'f' can be used as an ordinary function to call the operator() function.