Wa0028.
Wa0028.
container
A container can be described as an object that holds the same type of data.
Containers are used to implement different data structures such as arrays, lists,
trees, etc. The following is a container that provides the details of all the
containers along with the header file and the iterator type associated with it.
They:
Header
container illustrate Iterators
file
Deque Deque is Deque that allows insertion and removal from both ends. <deque> Random visits
Set A set is an associated container for storing a unique set. <set> bidirectional
multiset A multiset is an associated container for storing non-unique sets. <set> bidirectional
There are no
Stack Last-in, first-out (LIFO). <stack>
iterators
There are no
Queue First in, first out (FIFO). <queue>
iterators
Note:
Each container class contains a set of functions that can be used to manipulate
content.
ITERATOR
The iterator has two main functions: begin(): The member function begin()
returns the iterator to the first element of the Vector. end(): The member
function end() returns the iterator to the container's past-last element.
Iterator category
Input iterators are iterators that allow a program to read values from a
container. When we dereference the input iterator, we can read the value from
the container, but we don't change it. Input iterators are one-way iterators. The
input iterator can be incremented, but not decremented.
Output Iterator: The output iterator is similar to the input iterator, except that it
allows the program to modify the value of the container, but does not allow it to
be read. This is a one-way iterator. This is a write-only iterator.
Forward Iterator: The forward iterator uses the ++ operator to browse
containers. The forwarding iterator traverses each element of the container one
at a time, one element at a time.
Bidirectional Iterator: A bidirectional iterator is similar to a forward iterator,
except that it also moves backwards. It's a two-way iterator. It can be
incremented or decremented.
Random Access Iterator: A random access iterator can be used to access random
elements of a container. The Random Access Iterator has all the features of a
bidirectional iterator and also has an additional feature, which is pointer
addition. By adding an action using a pointer, we can access the random
elements of the container. Actions supported by iterators
element Incremental
Iterators Read write compare
access operations
v = *
Input -> ++ ==,!=
p
* p =
Output ++
v
v = * * p =
forward -> ++ ==,!=
p v
v = * * p =
bidirectional -> ++,- ==,!=
p v
Random v = * * p =
->,[] ++,-,+,-,+ =,-= ==,!=,<,>,<=,> =
visits p v
algorithm
Algorithms are functions that are used to process their contents in various
containers. Key points to remember:
Algorithms offer about 60 algorithmic functions to perform complex operations.
The standard algorithm allows us to process two different types of containers at
the same time.
The algorithm is not a member function of the container, but an independent
template function.
The algorithm saves a lot of time and effort.
If you want to access the STL algorithm, you must include a header file in the
program. STL algorithms can be categorized as: Non-mutated algorithms:
Non-mutated algorithms are algorithms that neither change any of the values of
container objects nor the order of the elements in which they appear. These
algorithms can be used for all container objects and make use of forward
iterators.
Mutation algorithm: A mutation algorithm is an algorithm that can be used to
change the value of a container. They can also be used to change the order in
which the elements in which they appear.
Sorting Algorithm: A sorting algorithm is a modified algorithm used to sort the
elements in a container.
Set Algorithm: Set up an algorithm is also known as a sort range algorithm. This
algorithm is used to perform certain functions on the container, which greatly
improves the efficiency of the program.
Relational algorithms : Relational algorithms are algorithms that are used to
process numerical data. They are primarily used to perform math operations on
all elements in a container.
Functional objects
d();
which is same as:
d.operator() ( );
Let's look at 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 : "<<result;
return 0;
}
Output:
Addition of a and b is : 10
In the example above, "f" is an object of the function_object class that contains
the definition of the operator() function. Therefore, 'f' can be used as a normal
function to call the operator() function.
Allocators in STL
std::allocator() in C++ with Examples
Allocators are objects responsible for encapsulating memory management. std::allocator is
used when you want to separate allocation and do construction in two steps. It is also used
when separate destruction and deallocation is done in two steps. All the STL containers in
C++ have a type parameter Allocator that is by default std::allocator. The default allocator
simply uses the operators new and delete to obtain and release memory. Declaration :
template <class T> class allocator;
Member functions associated with std::allocator() :
1. address: It is used for obtaining the address of an object although it is removed in
C++20.
2. construct: It is used to construct an object.It is also removed in C++20.
3. destroy: It is used to destruct an object in allocated storage.It is also removed in
C++20.
4. max_size: It returns the largest supported allocation size.It is deprecated in C++17
and removed in C++20.
5. allocate: Used for allocation of memory.
6. deallocate: Used for deallocation of memory.
Below programs illustrate the above mentioned function: Program 1:
● CPP
Output:
10
100