0% found this document useful (0 votes)
7 views33 pages

PPT13 Standard Template Library

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)
7 views33 pages

PPT13 Standard Template Library

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/ 33

UTA 018: OOPs

Standard Template Library (STL)


Standard Template Library (STL)
The Standard Template Library (STL) is a powerful library in C++ that
provides generic classes and functions designed to manage collections of
data and perform common operations on them. STL allows developers to
write efficient, reusable, and type-safe code. It enables the use of
containers, algorithms, and iterators to work with data structures in a
generic manner, making C++ more versatile for solving a wide range of
problems.

STL provide general-purpose classes & functions to implement


commonly used algorithms and data structures like vectors, lists, queues,
and stacks.
11/20/2024
Key Features of STL
• Generic Programming: STL uses templates to provide generic data
structures and algorithms.
• Efficiency: STL algorithms and containers are highly optimized for
performance.
• Type Safety: STL templates enforce type compatibility at compile-
time.
• Reusability: The same code can be reused for different data types
without modification.

11/20/2024
Components of the STL
• Containers – Classes that store collections of elements. Eg. Vector

• Algorithms – Functions that perform operations on data in


containers. Eg. sort()

• Iterator – Objects that enable traversal of elements in containers.


Generalised concept of pointers to point an element in a
container Eg. forward, bidirectional etc.

These components work together, with containers holding the data, iterators providing
a way to access the data, and algorithms performing operations on the data.

© Dr. Lokendra Vishwakarma


11/20/2024
[email protected]
Containers in STL
• Containers are objects that store collections of data. The STL provides
different types of containers, each optimized for specific tasks. STL
containers are divided into three categories:

• Sequence Containers: Maintain the order of elements based on insertion.


• Associative Containers: Automatically sort and store elements in a key-
value fashion.
• Container Adapters: Provide a modified interface for existing containers.

© Dr. Lokendra Vishwakarma


11/20/2024
[email protected]
Sequence Containers
Sequence containers store elements in a linear arrangement,
preserving the order in which elements are inserted.

Container Description Example Use Case


vector Dynamic array that supports fast random Storing lists of data with frequent appends.
access and efficient resizing at the end.
deque Double-ended queue, allows insertion and Managing data with access at both ends.
deletion at both ends.
list Doubly linked list, allows efficient insertion Use when frequent insertions/deletions are
and deletion anywhere in the list. needed.

© Dr. Lokendra Vishwakarma


11/20/2024
[email protected]
Sequence Containers: vector
• A vector is a dynamic array that can grow or shrink in size. It provides
random access to elements and supports efficient insertion and
deletion at the end.
Library: <vector>
Constructors: vector(), vector(size), vector(size, value)
Capacity Functions:

Function Description Function Description


size() Returns the number of elements capacity() Returns the total allocated memory
empty() Check if the vector is empty resize(new_size) Resizes the vector
reserve(capa Allocates space for the specified
city) capacity.
© Dr. Lokendra Vishwakarma
11/20/2024
[email protected]
Sequence Containers: vector
Modifiers Functions:
Function Description
push_back() Adds an element to the end.
pop_back() Removes the last element.
insert(position, value) Inserts an element at a given position.
erase(position) Removes an element from a specified position.
clear() Removes all elements.
emplace_back(arg…) Constructs and adds an element at the end.

Access Functions:
Function Description
at(index) Accesses the element at the specified position (bounds-checked).
front() Accesses the first element.
back() Accesses the last element.
© Dr. Lokendra Vishwakarma
11/20/2024 data() Returns a pointer to the underlying array.
[email protected]
Sequence Containers: vector (Example)

© Dr. Lokendra Vishwakarma


11/20/2024
[email protected]
Sequence Containers: deque
• A deque is a double-ended queue that allows insertion and deletion
at both the beginning and the end. It is implemented as a dynamic
array of fixed-size arrays, so accessing elements is almost as fast as in
a vector.
Library: <deque>
Constructors: deque(), deque(size), deque(size, value)
Capacity Functions:
size(), empty()

© Dr. Lokendra Vishwakarma


11/20/2024
[email protected]
Sequence Containers: deque
Modifier Functions:
Function Description
push_back(value), push_front(value) Adds elements to the end or beginning.
Pop_back(value), pop_front(value) Removes elements from the end or beginning.
insert(position, value) Inserts an element at a given position.
erase(position) Removes an element from a specified position.
clear() Removes all elements.

Access Functions:
Function Description
at(index) Accesses the element at the specified position (bounds-checked).
front() Accesses the first element.
back() Accesses the last element.
11/20/2024 data() Returns a pointer
© Dr. Lokendra to the underlying array.
Vishwakarma
[email protected]
Sequence Containers: deque (Example)

© Dr. Lokendra Vishwakarma


11/20/2024
[email protected]
Sequence Containers: deque (Example)

© Dr. Lokendra Vishwakarma


11/20/2024
[email protected]
Sequence Containers: list
• A list is a doubly linked list that allows efficient insertion and deletion
from anywhere in the list. However, it does not provide random
access to elements (like vectors and deques do).
Library: <list>
Constructors: list(), list(size), list(size, value)

Access Functions:
Function Description
front() Accesses the first element.
back() Accesses the last element.
© Dr. Lokendra Vishwakarma
11/20/2024
[email protected]
Sequence Containers: list
Modifier Functions:
Function Description
push_back(value), push_front(value) Adds elements to the end or beginning.
Pop_back(value), pop_front(value) Removes elements from the end or beginning.
insert(position, value) Inserts an element at a given position.
erase(position) Removes an element from a specified position.
clear() Removes all elements.
remove(value) Removes all elements equal to a value.
sort() Sorts the list.
reverse() Reverse the list

© Dr. Lokendra Vishwakarma


11/20/2024
[email protected]
Sequence Containers: list (Example)

© Dr. Lokendra Vishwakarma


11/20/2024
[email protected]
Differences Between deque and list
Feature List Deque
Underlying Structure Doubly linked list Array of fixed-size arrays
Random Access Not supported Supported with [] and at().
Efficient at any position (O(1) at Efficient at front and back, slower in
Insertion/Deletion
known position) the middle
Memory Usage Higher due to pointers per node More compact, contiguous blocks of
memory
Best Use Case Frequent middle Frequent front/back operations and
insertions/deletions, no need for occasional random access
random access

© Dr. Lokendra Vishwakarma


11/20/2024
[email protected]
Associative Containers
Associative containers store elements in a way that enables fast retrieval
based on keys. They use a binary tree or hash-based structure to keep
elements sorted and quickly searchable.

Container Description Example Use Case


set Stores unique elements in a sorted order. Keeping a unique list of IDs or sorted items.
Map Stores key-value pairs with unique keys, sorted Dictionary-like structure for fast lookups.
by key.
Multiset Stores sorted elements, allowing duplicates. Use when multiple identical elements are
needed.
Multimap Stores key-value pairs, allowing duplicate keys. Multi-value dictionary-like structure.

© Dr. Lokendra Vishwakarma


11/20/2024
[email protected]
Associative Containers: set
A set is an associative container that stores unique elements in a sorted
order. It does not allow duplicate elements.
Library: <set>
Constructors: set(), set(initializer_list)
Capacity Functions: size(), empty()
Modifier:
insert(value): Inserts a unique value.
erase(value), clear()
Lookup Functions:
find(value): finds an element.
count(value): check if an element is exist.
lower_bound(value), upper_bound(value)
© Dr. Lokendra Vishwakarma
11/20/2024
[email protected]
Associative Containers: set (Example)

© Dr. Lokendra Vishwakarma


11/20/2024
[email protected]
Associative Containers: set (Example)

© Dr. Lokendra Vishwakarma


11/20/2024
[email protected]
Associative Containers: map
A map is an associative container that stores key-value pairs in a sorted order by key. It
does not allow duplicate keys.
Library: <map>
Constructors: map(), map(initializer_list)
Capacity Functions: size(), empty()
Modifier:
insert(pair): Inserts a key-value pair.
erase(key), clear()
Lookup Functions:
find(key): finds a key.
count(key): check if a key is exist,
at(key): access the value by key,
operator[]: inserts or access the value by key
© Dr. Lokendra Vishwakarma
11/20/2024
[email protected]
Associative Containers: map (Example)

© Dr. Lokendra Vishwakarma


11/20/2024
[email protected]
Associative Containers: map (Example)
Example in DevC++

© Dr. Lokendra Vishwakarma


11/20/2024
[email protected]
Container Adaptors
Container adapters provide a restricted interface to the underlying container.
They adapt existing containers like deque and vector to provide specialized
behavior such as stack or queue operations.

Container Description Underlying Container Used


stack LIFO (Last In, First Out) container deque
queue FIFO (First In, First Out) container deque
priority_queue Elements are ordered by priority vector or deque

© Dr. Lokendra Vishwakarma


11/20/2024
[email protected]
Example of stack

© Dr. Lokendra Vishwakarma


11/20/2024
[email protected]
Iterators in STL
Iterators provide a way to access elements in containers. They behave like pointers, allowing
traversal of container elements. Iterators can be used with containers to make code generic
and flexible.

In C++, an iterator is an object that points to an element within a container. Think of an iterator
as a pointer that “points” to the location of an element but doesn’t directly hold the element's
value. Therefore, to access the actual value stored at the iterator’s position, you need to
dereference the iterator.

Types of Iterators
• Input Iterator: Can read data sequentially.
• Output Iterator: Can write data sequentially.
• Forward Iterator: Can read or write in a single direction.
• Bidirectional Iterator: Can move both forward and backward.
• Random Access Iterator: Can jump to any element in constant time.
© Dr. Lokendra Vishwakarma
11/20/2024
[email protected]
Using Iterators to Traverse Containers

© Dr. Lokendra Vishwakarma


11/20/2024
[email protected]
Traverse Containers using auto

© Dr. Lokendra Vishwakarma


11/20/2024
[email protected]
Algorithms in STL
The STL provides a variety of algorithms for operations such as sorting,
searching, counting, and modifying data. These algorithms work with
iterators, making them compatible with any container.

Algorithm Description
std::sort Sorts elements in a range in ascending order.
std::find Finds the first occurrence of a value in a range.
std::count Counts occurrences of a specific value in a range.
std::reverse Reverses the order of elements in a range.
std::accumulate Computes the sum of elements in a range.
© Dr. Lokendra Vishwakarma
11/20/2024
[email protected]
Example of Using Algorithms

© Dr. Lokendra Vishwakarma


11/20/2024
[email protected]
Function Objects (Functors)
• A function object or functor is an object that acts like a function. You
can create function objects by overloading the operator(). Functors
are often used as custom comparison functions in STL algorithms.

© Dr. Lokendra Vishwakarma


11/20/2024
[email protected]
Thank You

© Dr. Lokendra Vishwakarma


11/20/2024
[email protected]

You might also like