0% found this document useful (0 votes)
48 views14 pages

21pd39 - Standard Template Library Documentation

1. The document discusses the C++ Standard Template Library (STL) which provides common data structures and functions. 2. STL has four main components - containers, iterators, algorithms, and functions. Containers store and manage elements, iterators access container elements, algorithms perform operations on elements, and functions act as functors. 3. Common containers include vectors, lists, sets, and maps. Iterators include input, output, forward, bidirectional, and random access types. Algorithms perform tasks like sorting, searching, and partitioning. Functions act as functors that can contain state and be passed as template parameters.

Uploaded by

Varsha Senthil
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
48 views14 pages

21pd39 - Standard Template Library Documentation

1. The document discusses the C++ Standard Template Library (STL) which provides common data structures and functions. 2. STL has four main components - containers, iterators, algorithms, and functions. Containers store and manage elements, iterators access container elements, algorithms perform operations on elements, and functions act as functors. 3. Common containers include vectors, lists, sets, and maps. Iterators include input, output, forward, bidirectional, and random access types. Algorithms perform tasks like sorting, searching, and partitioning. Functions act as functors that can contain state and be passed as template parameters.

Uploaded by

Varsha Senthil
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 14

OBJECT ORIENTED

PROGRAMMING -
C++ ASSIGNMENT

-on Standard template


Library (STL)

NAME : VARSHA S
ROLL NO : 21PD39
DEPARTMENT : AMCS
BRANCH : DATA SCIENCE
SEMESTER : 2
THE C++ STANDARD TEMPLATE LIBRARY
(STL):

The Standard Template Library (STL) is a set


of C++ template classes to provide common
programming data structures and functions such as list,
stack, arrays, queues etc. It is a library of container
classes, algorithms, functions and iterators. It is a
generalized library and so the STL components are
parameterized. A working knowledge of template
classes is a prerequisite for working with STL.

STL has four components:


 Containers
 Iterators
 Algorithms
 Functions

1. Containers in C++ Standard Template Library

A container is a holder object that stores a collection


of other objects (its elements). They are implemented
as class templates, which allows great flexibility in the
types supported as elements. 
The container manages the storage space for its
elements and provides member functions to access
them, either directly or through iterators (reference
objects with similar properties to pointers). 

Containers are of four types:


 Sequence Containers
 Associative Containers
 Unordered associative Containers
 Container adaptors
1. Sequence Containers:
Sequence containers implement data structures
that can be accessed sequentially. 
a. Array
b. Vector
c. Deque
d. Forward list
e. List

2. Associative Containers:
Associative containers implement sorted data
structures that can be quickly searched (O(log n)
complexity).
a. Set
b. Map
c. Multiset
d. Multimap
3. Unordered Associative Containers:
Unordered associative containers implement
unsorted (hashed) data structures that can be quickly
searched ( O(1) amortized, O(n) worst-case
complexity). 

a. Unordered set
b. Unordered map
c. Unordered multiset
d. Unordered multimap

4. Container Adaptors:
Container adaptors provide a different
interface for sequential containers. 

a. Stack
b. Queue
c. Priority Queue

2. ITERATORS IN C++ STANDARD


TEMPLATE LIBRARY:

Iterators are used to point at the memory addresses


of STL containers. They are primarily used in
sequences of numbers, characters etc. They reduce the
complexity and execution time of the program.

OPERATIONS OF ITERATORS:
 begin()
 end()
 advance()
 next()
 prev()
 insertor()

1. begin()
This function is used to return the beginning
position of the container.
2. end()
This function is used to return the after end
position of the container.
3. advance()
This function is used to increment the iterator
position till the specified number mentioned in its
arguments.
4. next()
This function returns the new iterator that the
iterator would point after advancing the
positions mentioned in its arguments.
5. prev()
This function returns the new iterator that the
iterator would point after decrementing the
positions mentioned in its arguments.
6. insertor()
This function is used to insert the elements at
any position in the container. It accepts 2
arguments, the container and iterator to position
where the elements have to be inserted.

TYPES OF ITERATORS:
 Input Iterators
 Output Iterators
 Forward Iterators
 Bidirectional Iterators
 Random Access Iterators

1. INPUT ITERATORS :
Input iterators are the iterators that can be used in
sequential input operations, where each value pointed
by the iterator is read-only once and then the iterator
is incremented.

2. OUTPUT ITERATORS:
Output iterators are considered to be the exact
opposite of input iterators, as they perform the
opposite function of input iterators.

3. FORWARD ITERATORS :
Forward iterators are considered to be
the combination of input as well as output
iterators. It provides support to the functionality of
both of them. It permits values to be both accessed
and modified.

4. BIDIRECTIONAL ITERATORS :
Bidirectional iterators are iterators that can be
used to access the sequence of elements in a range
in both directions (towards the end and towards the
beginning). They are similar to forward iterators,
except that they can move in the backward
direction also, unlike the forward iterators, which can
move only in the forward direction.

5. RANDOM - ACCESS ITERATORS :


Random-access iterators are iterators that can be used
to access elements at an arbitrary offset
position relative to the element they point to, offering
the same functionality as pointers. Random-access
iterators are the most complete iterators in terms of
functionality. All pointer types are also valid
random-access iterators.
fig 1.1

From fig 1.1 we came to know that 


 forward, bidirectional and random-access 
iterators are also valid input iterators, as well as
output iterators
  bidirectional and random-access iterators are
also valid forward iterators
 random-access iterators are also valid
bidirectional iterators
 Finally we conclude that random-access iterators
are the strongest of all iterator types.

3. ALGORITHMS IN C++ STANDARD TEMPLATE


LIBRARY:

The header algorithm defines a collection of functions


especially designed to be used on ranges of elements.
They act on containers and provide means for various
operations for the contents of the containers. STL has
an ocean of algorithms, for all we have to use <
algorithm > library functions.
It has approximately 60 algorithm functions to
perform the complex operations.

Algorithm Operations
 Sorting
 Searching
 Important STL algorithms
 Useful Array Algorithms
 Partition Operations

There are two types of algorithms:


 Non- Manipulating algorithm

 Manipulating algorithm

Non – Manipulating algorithm:


Syntax :
1. sort (first_iterator, last_iterator) 
2. reverse (first_iterator, last_iterator) 
3. *max_element (first_iterator, last_iterator
4. *min_element (first_iterator, last_iterator
5. accumulate(first_iterator,
last_iterator,initial value of sum)
6. count(first_iterator, last_iterator,x)
7. find(first_iterator, last_iterator, x) 
8. binary_search(first_iterator, last_iterator,
x) 
9. lower_bound(first_iterator, last_iterator,
x)
10. upper_bound(first_iterator,
last_iterator, x) 

Manipulating algorithm:
Syntax :
1. arr.erase(position to be deleted) 
2. arr.erase(unique(arr.begin(),arr.end()),arr.end(

3. next_permutation(first_iterator, last_iterator) 
4.  prev_permutation(first_iterator, last_iterator)
5. distance(first_iterator,desired_position) 
Apart from these there are many more algorithm
operations in STL.
EXAMPLE:
sort (first_iterator, last_iterator) :
This function internally uses IntroSort. In more details
it is implemented using hybrid of QuickSort, HeapSort
and InsertionSort. By default, it uses QuickSort but if
QuickSort is doing unfair partitioning and taking more
than N*logN time, it switches to HeapSort and when
the array size becomes really small, it switches to
InsertionSort. 
EXAMPLE CODE:
// C++ program to sort an array

#include <algorithm> //algorithm header


#include <iostream>
 
using namespace std;
 
void display(int a[], int array_size)
{
    for (int i = 0; i < array_size; ++i)
        cout << a[i] << " ";
}
 
int main()
{
    int a[] = { 1, 5, 8, 9, 6, 7, 3, 4, 2, 0 };
   
    
    int asize = sizeof(a) / sizeof(a[0]);

    cout << "The array before sorting is : \n";


   
    // print the array before sorting
    display(a, asize);
 
      // sort algo
    sort(a, a + asize);
 
    cout << "\n\nThe array after sorting is :\n";
   
    // print the array after sorting
    display(a, asize);
 
    return 0;
}
https://www.geeksforgeeks.org/algorithms-library-c-stl/

FUNCTIONS IN C++ STANDARD TEMPLATE


LIBRARY:
The STL includes classes that overload the function call
operator. Instances of such classes are called function
objects or functors.
Functors allow the working of the associated function to
be customized with the help of parameters to be passed.
A function object, or functor, is any type that implements
operator(). This operator is referred to as the call
operator or sometimes the application operator. The C++
Standard Library uses function objects primarily as
sorting criteria for containers and in algorithms.
Function objects provide two main advantages over a
straight function call. The first is that a function object
can contain state. The second is that a function object is a
type and therefore can be used as a template parameter.
Creating a Function Object :
To create a function object, create a type and implement
operator(), such as ;
class Functor
{
public:
int operator()(int a, int b)
{
return a + b;
}
};

int main()
{
Functor f;
int a = 5;
int b = 7;
int ans = f(a, b);
cout<<”The addition of two numbers
are”<<ans;
return 0;
}

The last line of the main function shows how you call the


function object. This call looks like a call to a function, but
it's actually calling operator() of the Functor type. This
similarity between calling a function object and a function is
how the term function object came about.
The above code is the example code for function object.
BIBILIOGRAPHY:

https://www.geeksforgeeks.org/the-c-standard-
template-library-stl/
https://www.tutorialspoint.com/cplusplus/
cpp_stl_tutorial.htm
https://www.geeksforgeeks.org/containers-cpp-stl/
https://www.javatpoint.com/cpp-iterators
https://www.geeksforgeeks.org/iterators-c-stl/
https://cplusplus.com/reference/algorithm/
https://www.geeksforgeeks.org/sort-algorithms-the-c-
standard-template-library-stl/
https://docs.microsoft.com/en-us/cpp/standard-
library/function-objects-in-the-stl?
view=msvc-170
https://www.geeksforgeeks.org/functors-in-cpp/
THANK YOU

You might also like