
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Upper Bound and Lower Bound for Non-Increasing Vector in C++
In this article we are going to discuss the vector::upper_bound() and vector::lower_bound() for an array sorted in non-increasing order in C++ STL.
Vectors are similar to the dynamic arrays; they have the ability to modify its size itself whenever a value is inserted into or removed from the container where we are storing the value.
In a Vector, lower bound returns an iterator pointing to the first element in the range that does not compare the given value. Upper Bound returns an iterator pointing element in the range that smaller than given value.
Input
30 30 30 20 20 20 10 10
Output
Lower bound of 20= 3 Upper bound of 20= 6
Input
9 9 8 8 8 7 7 7 6 6 6 6
Output
Lower bound of 7= 5 Upper bound of 7= 8
Return value
It returns an iterator pointing to the first element of the range and also returns and iterator pointing to the last element of the range.
Approach that can be followed
Firstly, we initialize the vector.
Then we sort the vector element in non-increasing order.
Then we find its lower bound.
Then we find its upper bound.
At last we print both the bounds.
By using above approach, we can find out the lower bound and upper bound of any vector, it is necessary to sort the vector to find it lower and upper bound. If the vector is not sorted, then we cannot find its bound
Example
/ / C++ program to demonstrate the working of lower bound and upper bound #include<iosteam.h> #include<vector.h> Using namespace std; int main ( ){ int vect[ ] = {13,13,13,16,16,16,17,17,17,17,18,18} vector<int> v(vect, vect+8); sort (v.begin( ), v.end( ), greater<int>( )); cout<< “ \n Sorted Vector: ”; for( auto i = vect.begin( ), i =! vect.end( ), ++i) vector<int> iterator low, up; low = lower_bound (v.begin( ), v.end( ), 17); up = upper_bound (v.begin( ), v.end( ), 17); cout<<” Lower bound” << (lower – v.begin( ))<< “ \n”; cout<< “ Upper bound “<< (upper – v,begin( ))<<”\n”; return 0; }
Output
If we run the above code it will generate the following output
Sorted Vector: 18 18 17 17 17 17 16 16 16 13 13 13 Lower bound = 2 Upper bound = 6
Example
#include<iosteam.h> #include<vector.h> Using namespace std; int main ( ){ int vect[ ] = {5,5,5,5,7,7 7,8,8,8,8,9,9,9,10,10} vector<int> v(vect, vect+16); sort (v.begin( ), v.end( )); cout<< “ \n Sorted Vector: ”; for( auto i = vect.begin( ), i =!vect.end( ), ++i) vector<int> iterator low, up; low = lower_bound (v.begin( ), v.end( ), 8); up = upper_bound (v.begin( ), v.end( ), 8); cout<<” Lower bound” << (lower – v.begin( ))<< “ \n”; cout<< “ Upper bound “<< (upper – v,begin( ))<<”\n”; return 0; }
Output
If we run the above code it will generate the following output
Sorted Vector: 10 10 9 9 9 8 8 8 8 7 7 7 5 5 5 5 Lower bound = 5 Upper bound = 9