Upload
joyjit-choudhury
View
81
Download
0
Embed Size (px)
Citation preview
Level up your coding skills with
the C++ Standard Template
Library (STL):
Algorithms: II
BY JOYJIT CHOUDHURY
Binary Search
binary_search(), searches for an element equivalent to
val in the range [first, last)
It returns true if any such element exists, else returns false
The elements are compared using, either operator< (by
default) or comp (binary predicate)
The elements in the range [first, last), shall already be
sorted according to this same criterion (either operator< or
comp)
Binary Search
Time Complexity : O(logN), on containers which provide
random access iterators: vector, deque, array
Time Complexity : O(N), on containers which provide non-
random access iterators: forward list, list
Defined in the header <algorithm>
Belongs to the namespace std
Lower Bound
lower_bound(), returns an iterator pointing to the first
element in the range [first,last) which is not less than
(i.e. greater or equal to) the val
If all the element in the range compare less than val,
the function returns last
The elements are compared using, either operator< (by
default) or comp (binary predicate)
The elements in the range [first, last), shall already be
sorted according to this same criterion (either operator< or
comp)
Lower Bound
Time Complexity : O(logN), on containers which provide
random access iterators: vector, deque, array
Time Complexity : O(N), on containers which provide non-
random access iterators: forward list, list
Defined in the header <algorithm>
Belongs to the namespace std
myV<int>
myV.begin() myV.end()
x
auto it = lower_bound(myV.begin(),myV.end(),100);
myV<int> xit = myV.end()
Upper Bound
upper_bound(), returns an iterator pointing to the first
element in the range [first,last) which is greater than val
If no element in the range compares greater than val,
the function returns last
The elements are compared using, either operator< (by
default) or comp (binary predicate)
The elements in the range [first, last), shall already be
sorted according to this same criterion (either operator< or
comp)
Upper Bound
Time Complexity : O(logN), on containers which provide
random access iterators: vector, deque, array
Time Complexity : O(N), on containers which provide non-
random access iterators: forward list, list
Defined in the header <algorithm>
Belongs to the namespace std
myV<int>
myV.begin() myV.end()
x
auto it = upper_bound(myV.begin(),myV.end(),100);
myV<int> xit = myV.end()
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!