SlideShare a Scribd company logo
Level up your coding skills with
the C++ Standard Template
Library (STL):
Algorithms: I
BY JOYJIT CHOUDHURY
Algorithms
 In the C++ Standard Library, algorithms are components
that perform algorithmic operations on containers and
other sequences
 The C++ standard provides some standard algorithms
collected in the <algorithm> standard header. A handful
of algorithms are also in the <numeric> header
 All algorithms are in the std namespace
Sort
 sort(), sorts the elements in the range [first, last)
 The sort is performed by, either using the < operator
(resulting in an ascending order), or a Boolean “compare
function” (aka. Binary Predicate) passed as an argument
to the sort function.
 Time Complexity : O(N*logN)
 Works on containers which provide random access
iterators: vector, deque, array
 Defined in the header <algorithm>
 Belongs to the namespace std
Algorithms: I
So, what is this ‘Binary Predicate’?
 Sorting is a result of comparisons. This “compare - binary predicate”
decides where the elements are put after each comparison
 Think of it as a function, which is passed any two elements from the
sequence and this function returns a true or false, depending on
which the relative ordering (which element in the pair comes first, and
which comes second) of each of these pair of elements in the final
sorted sequence, is defined
 In binary predicate, binary stands for the fact that it accepts two
elements as input, and predicate indicates that it produces a boolean
output, i.e. true or false
 This binary predicate is passed two elements of the type
contained in sequence. It returns true if the first
argument goes before the second argument in the
ordering it defines, false otherwise.
 The binary predicate in the form of a function is given
here
 Let’s take for example:
 Now we create a vector containing objects of stuff class and insert a few values :
a = 23
b = 34
a = 43
b = 2
a = 23
b = 98
a = 1
b = 2
a = 45
b = 54myV
 Sort the elements of myV in the descending order of it’s data member int b
 The global function compare :
 After calling sort() and passing compare as the third argument :
a = 23
b = 98
a = 45
b = 54
a = 23
b = 34
a = 43
b = 2
a = 1
b = 2
myV
struct as a binary predicate
 A function isn’t the only way to define a binary
predicate
 It can also be done by creating a struct
Defining the struct’s ()operator (which is done similar to
a function definition)
And then passing an object of this struct as the third
parameter to sort()
 From the previous example, the binary predicate could
also be defined as a struct
Algorithms: I
Using templates
 Suppose you want to sort a vector in descending order, it
might contain data of int, float or char or some other type
 Do you write separate functions/structs for the different types
?
 No. You can use templates to create a generic
function/struct and use it accordingly
Algorithms: I
 Achieving a descending order of elements in a container can be
made even easier by the use of std::greater
 It’s a binary function object class whose call returns whether the its
first argument compares greater than the second (as returned by
operator >)
 Which is exactly what our compare function/struct did in the
previous example
 That’s not all. There are many other functions
and techniques that could come in handy.
Read about them on cplusplus.com or
cppreference.com or somewhere else. Just
google it!
Algorithms: I

More Related Content

PDF
Stack & Queue
PDF
Algorithms: II
PDF
Priority Queue
PDF
PDF
An Introduction to the C++ Standard Library
PDF
PDF
Array data structure
PPTX
Vector class in C++
Stack & Queue
Algorithms: II
Priority Queue
An Introduction to the C++ Standard Library
Array data structure
Vector class in C++

What's hot (20)

PDF
PPT
02 Arrays And Memory Mapping
PPTX
Data structure using c module 1
PPTX
Data structures
PPTX
Data structure , stack , queue
PPT
2CPP16 - STL
PPTX
Stack and Queue
PPT
List in java
PDF
STL in C++
PPT
Arrays Data Structure
PDF
LectureNotes-06-DSA
PPT
Stack Implementation
PPTX
FSTREAM,ASSERT LIBRARY & CTYPE LIBRARY.
PPT
Array Presentation
PPTX
Java Arrays and DateTime Functions
PPTX
Data Structures - Lecture 3 [Arrays]
PPTX
Standard Template Library
PPT
Data Structure In C#
PPT
Array Presentation (EngineerBaBu.com)
PPT
Stl Containers
02 Arrays And Memory Mapping
Data structure using c module 1
Data structures
Data structure , stack , queue
2CPP16 - STL
Stack and Queue
List in java
STL in C++
Arrays Data Structure
LectureNotes-06-DSA
Stack Implementation
FSTREAM,ASSERT LIBRARY & CTYPE LIBRARY.
Array Presentation
Java Arrays and DateTime Functions
Data Structures - Lecture 3 [Arrays]
Standard Template Library
Data Structure In C#
Array Presentation (EngineerBaBu.com)
Stl Containers
Ad

Viewers also liked (11)

PDF
PDF
PDF
SPOJ Problem: OLOLO
PPTX
ĐżĐŸŃ€Ń‚Ń„ĐŸĐ»Ń–ĐŸ ĐČŃ‡ĐžŃ‚Đ”Đ»Ń ĐŒĐ°Ń‚Đ”ĐŒĐ°Ń‚ĐžĐșĐž
PDF
Elementos da teoria da integração
PPTX
PRESENT PERFECT
PPTX
3Com 3C95116M-TS-T
PPTX
ĐșĐŸĐœŃ ĐżĐŸ ĐœĐ”Ń‚Ń€Ń‚Đ”Ń…Đœ рос ĐŒŃƒŃ€Đ°ĐČŃŒĐ”ĐČа
 
PPTX
Grupos cooperativos
PDF
The case of_the_killer_robot
PPTX
Laboratory Management Software
SPOJ Problem: OLOLO
ĐżĐŸŃ€Ń‚Ń„ĐŸĐ»Ń–ĐŸ ĐČŃ‡ĐžŃ‚Đ”Đ»Ń ĐŒĐ°Ń‚Đ”ĐŒĐ°Ń‚ĐžĐșĐž
Elementos da teoria da integração
PRESENT PERFECT
3Com 3C95116M-TS-T
ĐșĐŸĐœŃ ĐżĐŸ ĐœĐ”Ń‚Ń€Ń‚Đ”Ń…Đœ рос ĐŒŃƒŃ€Đ°ĐČŃŒĐ”ĐČа
 
Grupos cooperativos
The case of_the_killer_robot
Laboratory Management Software
Ad

Similar to Algorithms: I (20)

DOCX
C questions
DOCX
New microsoft office word document (2)
PPSX
Object oriented concepts & programming (2620003)
PDF
C++ Interview Question And Answer
PDF
C++ questions And Answer
PDF
C++ Object oriented concepts & programming
PDF
Number 1 I have completed and number 6 is just uploading I .pdf
PPTX
Whats New In C# 4 0 - NetPonto
PPT
Classes & objects new
DOCX
Notes on c++
PDF
Module IV_updated(old).pdf
DOC
1183 c-interview-questions-and-answers
PPTX
Lecture 08.pptx
PPT
C++ Interview Questions
PPTX
Unit ii
PDF
Python_Unit_2.pdf
PDF
functions- best.pdf
PDF
Unit iii vb_study_materials
PPTX
Constructor and destructor
PDF
Class notes(week 3) on class objects and methods
C questions
New microsoft office word document (2)
Object oriented concepts & programming (2620003)
C++ Interview Question And Answer
C++ questions And Answer
C++ Object oriented concepts & programming
Number 1 I have completed and number 6 is just uploading I .pdf
Whats New In C# 4 0 - NetPonto
Classes & objects new
Notes on c++
Module IV_updated(old).pdf
1183 c-interview-questions-and-answers
Lecture 08.pptx
C++ Interview Questions
Unit ii
Python_Unit_2.pdf
functions- best.pdf
Unit iii vb_study_materials
Constructor and destructor
Class notes(week 3) on class objects and methods

Recently uploaded (20)

PDF
Adobe Illustrator 28.6 Crack My Vision of Vector Design
PDF
Odoo Companies in India – Driving Business Transformation.pdf
PPTX
VVF-Customer-Presentation2025-Ver1.9.pptx
PDF
medical staffing services at VALiNTRY
PPTX
Agentic AI : A Practical Guide. Undersating, Implementing and Scaling Autono...
PPTX
CHAPTER 12 - CYBER SECURITY AND FUTURE SKILLS (1) (1).pptx
PDF
Digital Strategies for Manufacturing Companies
PDF
T3DD25 TYPO3 Content Blocks - Deep Dive by André Kraus
PDF
2025 Textile ERP Trends: SAP, Odoo & Oracle
PPTX
ISO 45001 Occupational Health and Safety Management System
PPTX
history of c programming in notes for students .pptx
PDF
Claude Code: Everyone is a 10x Developer - A Comprehensive AI-Powered CLI Tool
PPTX
Transform Your Business with a Software ERP System
PPTX
CHAPTER 2 - PM Management and IT Context
PPTX
L1 - Introduction to python Backend.pptx
PDF
Design an Analysis of Algorithms II-SECS-1021-03
PDF
Wondershare Filmora 15 Crack With Activation Key [2025
PDF
Design an Analysis of Algorithms I-SECS-1021-03
PDF
Navsoft: AI-Powered Business Solutions & Custom Software Development
PDF
SAP S4 Hana Brochure 3 (PTS SYSTEMS AND SOLUTIONS)
Adobe Illustrator 28.6 Crack My Vision of Vector Design
Odoo Companies in India – Driving Business Transformation.pdf
VVF-Customer-Presentation2025-Ver1.9.pptx
medical staffing services at VALiNTRY
Agentic AI : A Practical Guide. Undersating, Implementing and Scaling Autono...
CHAPTER 12 - CYBER SECURITY AND FUTURE SKILLS (1) (1).pptx
Digital Strategies for Manufacturing Companies
T3DD25 TYPO3 Content Blocks - Deep Dive by André Kraus
2025 Textile ERP Trends: SAP, Odoo & Oracle
ISO 45001 Occupational Health and Safety Management System
history of c programming in notes for students .pptx
Claude Code: Everyone is a 10x Developer - A Comprehensive AI-Powered CLI Tool
Transform Your Business with a Software ERP System
CHAPTER 2 - PM Management and IT Context
L1 - Introduction to python Backend.pptx
Design an Analysis of Algorithms II-SECS-1021-03
Wondershare Filmora 15 Crack With Activation Key [2025
Design an Analysis of Algorithms I-SECS-1021-03
Navsoft: AI-Powered Business Solutions & Custom Software Development
SAP S4 Hana Brochure 3 (PTS SYSTEMS AND SOLUTIONS)

Algorithms: I

  • 1. Level up your coding skills with the C++ Standard Template Library (STL): Algorithms: I BY JOYJIT CHOUDHURY
  • 2. Algorithms  In the C++ Standard Library, algorithms are components that perform algorithmic operations on containers and other sequences  The C++ standard provides some standard algorithms collected in the <algorithm> standard header. A handful of algorithms are also in the <numeric> header  All algorithms are in the std namespace
  • 3. Sort  sort(), sorts the elements in the range [first, last)  The sort is performed by, either using the < operator (resulting in an ascending order), or a Boolean “compare function” (aka. Binary Predicate) passed as an argument to the sort function.  Time Complexity : O(N*logN)  Works on containers which provide random access iterators: vector, deque, array  Defined in the header <algorithm>  Belongs to the namespace std
  • 5. So, what is this ‘Binary Predicate’?  Sorting is a result of comparisons. This “compare - binary predicate” decides where the elements are put after each comparison  Think of it as a function, which is passed any two elements from the sequence and this function returns a true or false, depending on which the relative ordering (which element in the pair comes first, and which comes second) of each of these pair of elements in the final sorted sequence, is defined  In binary predicate, binary stands for the fact that it accepts two elements as input, and predicate indicates that it produces a boolean output, i.e. true or false
  • 6.  This binary predicate is passed two elements of the type contained in sequence. It returns true if the first argument goes before the second argument in the ordering it defines, false otherwise.  The binary predicate in the form of a function is given here
  • 7.  Let’s take for example:  Now we create a vector containing objects of stuff class and insert a few values : a = 23 b = 34 a = 43 b = 2 a = 23 b = 98 a = 1 b = 2 a = 45 b = 54myV
  • 8.  Sort the elements of myV in the descending order of it’s data member int b  The global function compare :  After calling sort() and passing compare as the third argument : a = 23 b = 98 a = 45 b = 54 a = 23 b = 34 a = 43 b = 2 a = 1 b = 2 myV
  • 9. struct as a binary predicate  A function isn’t the only way to define a binary predicate  It can also be done by creating a struct Defining the struct’s ()operator (which is done similar to a function definition) And then passing an object of this struct as the third parameter to sort()
  • 10.  From the previous example, the binary predicate could also be defined as a struct
  • 12. Using templates  Suppose you want to sort a vector in descending order, it might contain data of int, float or char or some other type  Do you write separate functions/structs for the different types ?  No. You can use templates to create a generic function/struct and use it accordingly
  • 14.  Achieving a descending order of elements in a container can be made even easier by the use of std::greater  It’s a binary function object class whose call returns whether the its first argument compares greater than the second (as returned by operator >)  Which is exactly what our compare function/struct did in the previous example
  • 15.  That’s not all. There are many other functions and techniques that could come in handy. Read about them on cplusplus.com or cppreference.com or somewhere else. Just google it!