2. What is a
Standard
Library ?
In the C++ programming language, the Standard Library
is a collection of classes and functions.
The C++ Standard Library can be categorized into two
parts −
1. The Standard Function Library
2. The Object-Oriented Class Library
3. What are
Templates ?
• The simple idea is to pass data type as a
parameter so that we don’t need to write the
same code for different data types.
• C++ adds two new keywords to support
templates: ‘template’ and ‘typename’. The
second keyword can always be replaced by
keyword ‘class’.
4. How Templates work
• Templates are expanded
at compiler time.
• The difference is, compiler
does type checking before
template expansion.
• The source code contains
only function/class, but
compiled code may
contain multiple copies of
same function/class.
5. What is
Standard
Template
Library ?
The Standard Template Library (STL) is a
set of C++ template classes to provide
common programming data structures and
functions such as lists, stacks, arrays, etc.
A working knowledge of template
classes is a prerequisite for working with
STL.
7. STL Component Overview
Data Storage
Containers
• Hold Data
Iterators
• Access Data
Algorithms
• Manipulates Data
Data Access
Algorithms
8. Containers
• Containers or container classes store
objects and data.
• There are in total seven standard “first-
class” container classes and three
container adaptor classes and only
seven header files that provide access to
these containers or container adaptors.
11. What is Vector
Class?
• Vector is a template class in STL (Standard Template
Library) of C++ programming language.
• These are sequence containers that store elements.
• They can automatically manage storage. It is efficient if you
add and delete data often.
12. Some Methods of Vector Class
#include <vector>
To import Vector Class
size( )
Number of
Elemets
push_back( )
Appends at end
pop_back( )
Erase last
Element
begin( )
Reference to
first element
end( )
Reference to
end of Vector
14. What is a List?
• Sequence Containers - allow constant time insert and erase
operations.
• Store elements they contain in different and unrelated
storage locations.
• Lists - better in inserting, extracting and moving elements
in any position within the container for which an iterator
has already been obtained, and therefore also in
algorithms that make intensive use of these, like sorting
algorithms.
• Consume some extra memory to keep the linking
information associated to each element.
15. Some
Operations
of List Class
Operations Uses
sort Sort elements in a list
splice Transfer elements from 1 list to another
list
merge Merge sorted lists
unique Removes duplicate elements
swap Exchange the content of 1 list with that
of another list.
assign Assigns new contents to the list
remove Erase all instances of value
17. Deque Class
• Sequence containers with the feature of expansion and
contraction on both the ends.
• Implemented as dynamic arrays.
• To use deque container in a program include statement:
#include <deque>
• Syntax: Deque declaration
deque <elementType> name(size);
18. Some
Operations of
Deque Class
Operation Description
insert Inserts an element.
maxsize Return the max no of elements.
pushfront Push elements into a deque from the front.
pushback Push elements into a deque from the back.
popfront Pop/remove elements from a deque from
the front.
popback Pop/remove elements from a deque from
the back.
clear Remove all the elements of the deque
container.
20. Types of
Container
Adaptors
• Queue: A queue can store elements in the sequence
container list or deque data structure.
• Priority queue: The priority queue adapter class is a little
bit different in the sense that it enables the insertion of
elements in sorted order into the data structure and
removal of elements occurs from the front of the waiting
line.
• Stack: The class called stack is an implementation of the
stack data structure where insertion and retrieval of
elements occurs according to the FIFO manner
23. Iterators • What are iterators?
• How many types of iterators are provided by
C++ ?
• Can iterators be null in C++ ?
24. Iterators
17
17
23
12
17
4
array
size 4
Iterators are pointer like entries that are
used to access individual elements in the
container
Often, they are used to move
sequentially from element to element, a
process called iterating through
container.
26. Algorithms
• 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.
• Range - Sequence of objects that can be accessed
through iterators or pointers, such as an array or an
instance of some of the STL containers.
27. Some of the most used Algorithms
Sort
Algorithm
Reverse
Algorithm
Max element
Algorithm
Min element
Algorithm
Accumulate
Algorithm
Find
Algorithm
Count
Algorithm
28. Sort Algorithm
Sort algorithm - To sort a given
Vector
sort Prototype -
sort(first_iterator, last_iterator)
Example –
vector <int> V{ 10,20,40,15};
sort(V.begin(),V.end());
29. Reverse Algorithm
Reverse Algorithm – To reverse
a given vector
Reverse Prototype –
reverse(first_iterator,
last_iterator)
Example –
vector <int> V{ 12,8,9,15,7};
reverse(V.begin(),V.end());
30. Max element
Algorithm
Max element Algorithm – To find
the maximum element of a given
vector.
*max_element Prototype -
*max_element(first_iterator,
last_iterator)
Example –
vector <int> V{9,100,4,-98};
*max_element(V.begin(),V.end());
31. Min element
Algorithm
Min element Algorithm – To find
the minimum element of a
given vector.
*min_element Prototype –
*min_element(first_iterator,
last_iterator)
Example –
vector <int> V{54,345,23,1,-23};
*min_element(V.begin(),V.end());
32. Accumulate
Algorithm
Accumulate Algorithm – Returns
the summation of given Vector
Elements
accumulate Prototype –
accumulate(first_iterator,
last_iterator, initial value of sum)
Example –
vector <int> V{ 9,78,82,-8,0,12};
accumulate(V.begin(),V.end(),0);
33. Find Algorithm
Find Algorithm – Find value in a given range and
returns the iterator to the first occurrences of
value in each vector and points to last address of
vector V.end() if element is not present in the
same vector
find Prototype – find(initial_iterator,
final_iterator,value)
Example –
vector <int> V{ 8,7,-81,9,0,78};
find(V.begin(),V.end(),7);
34. Count Algorithm
Count Algorithm – returns the count of
occurrences of value in a given vector
count Prototype –
count(initial_iterator,
final_iterator,value)
Example –
vector <int> V{8,34,23,34,234,23,4,34};
count(V.begin(),V.end(),34);
35. References
• David Vandevoorde and Nicolai M. Josuttis (2002). C++
Templates: The Complete Guide. Addison-Wesley
Professional. ISBN 0-201-73484-2.
• Nicolai M. Josuttis (2000). The C++ Standard Library: A
Tutorial and Reference. Addison-Wesley. ISBN 0-201-37926-
0.
• Atul Saini and David R. Musser. STL Tutorial and Reference
Guide: C+ + Programming with the Standard Template
Library.