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
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
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
 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

Algorithms: I

  • 1.
    Level up yourcoding skills with the C++ Standard Template Library (STL): Algorithms: I BY JOYJIT CHOUDHURY
  • 2.
    Algorithms  In theC++ 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(), sortsthe 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 isthis ‘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 binarypredicate 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 takefor 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 theelements 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 abinary 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 theprevious example, the binary predicate could also be defined as a struct
  • 12.
    Using templates  Supposeyou 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 adescending 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 notall. 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!