0% found this document useful (0 votes)
18 views8 pages

Wa0028.

The document provides an overview of C++ Standard Template Library (STL) containers, iterators, algorithms, function objects, and allocators. It describes various container types such as vector, list, and map, along with their associated header files and iterator types. Additionally, it explains the role of iterators in accessing container elements, the categorization of algorithms, and the purpose of allocators in memory management.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
18 views8 pages

Wa0028.

The document provides an overview of C++ Standard Template Library (STL) containers, iterators, algorithms, function objects, and allocators. It describes various container types such as vector, list, and map, along with their associated header files and iterator types. Additionally, it explains the role of iterators in accessing container elements, the categorization of algorithms, and the purpose of allocators in memory management.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

C++ STL

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

A vector is a class that creates a dynamic array that allows insertion


Vector <vector> Random visits
and deletion later

A list is a sequence container that allows insertion and deletion from


List <list> bidirectional
any location.

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

A map is an association container for storing unique key-value pairs,


Map <map> bidirectional
i.e., each key is associated with only one value (one-to-one mapping).

A multimap is an associative container for storing key-value pairs,


multimap <map> bidirectional
each of which can be associated with multiple values.

There are no
Stack Last-in, first-out (LIFO). <stack>
iterators

There are no
Queue First in, first out (FIFO). <queue>
iterators

Priority-qu There are no


The first element is always the highest priority. <queue>
eue iterators

Container Classification: Sequence Container Association Container


Derived Container

Note:
Each container class contains a set of functions that can be used to manipulate

content.

ITERATOR

An iterator is a pointer-like entity that accesses individual elements in a


container.
Iterators move sequentially from one element to another. This process is called
traversing the container.

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

There are five main types of iterators: Input Iterators:

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

A function object is a function that is wrapped in a class, so it looks like an


object. Functional objects extend the characteristics of general functionality by
using object-oriented (e.g., generic programming) functionality. Therefore, it
can be said that a function object is a smart pointer that has many advantages
over ordinary functions. The following are the advantages of function objects
over regular functions:
Function objects can have member functions as well as member properties.
You can initialize a function object before using it.
Regular functions can have different types only if the signatures are different.
Function objects can have different types, even if the signals are naturally the
same.
Function objects are faster than regular functions. Function objects are also
referred to as function keys. A function object is an object that contains at least
one definition of the operator() function. This means that if you declare object
'd' of a class that defines the operator() function, you can use object 'd' as a
regular function. Assuming that "d" is the object of the class, the operator()
function can be called:

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

// C++ program for illustration


// of std::allocator() function
#include <iostream>
#include <memory>
using namespace std;
int main()
{
// allocator for integer values
allocator<int> myAllocator;
// allocate space for five ints
int* arr = myAllocator.allocate(5);
// construct arr[0] and arr[3]
// myAllocator.construct(arr, 100); // no longer allowed in C++20
arr[0] = 100; // do this instead
arr[3] = 10;
cout << arr[3] << endl;
cout << arr[0] << endl;
// deallocate space for five ints
myAllocator.deallocate(arr, 5);
return 0;
}

Output:
10
100

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy