// CPP program to illustrate
// std :: lower_bound
#include <bits/stdc++.h>
// Driver code
int main()
{
// Input vector
std::vector<int> v{ 10, 20, 30, 30, 30, 40, 50 };
// Print vector
std::cout << "Vector contains :";
for (unsigned int i = 0; i < v.size(); i++)
std::cout << " " << v[i];
std::cout << "
";
std::vector<int>::iterator low1, low2, low3;
// std :: lower_bound
low1 = std::lower_bound(v.begin(), v.end(), 30);
low2 = std::lower_bound(v.begin(), v.end(), 35);
low3 = std::lower_bound(v.begin(), v.end(), 55);
// Printing the lower bounds
std::cout
<< "
lower_bound for element 30 at position : "
<< (low1 - v.begin());
std::cout
<< "
lower_bound for element 35 at position : "
<< (low2 - v.begin());
std::cout
<< "
lower_bound for element 55 at position : "
<< (low3 - v.begin());
return 0;
}
Iterator lower_bound (Iterator first, Iterator last, const val)
lower_bound returns an iterator pointing to the first element in the range [first,last) which has a value not less than ‘val’.
Iterator upper_bound (Iterator first, Iterator last, const val)
upper_bound returns an iterator pointing to the first element in the range [first,last) which has a value greater than ‘val’.
If the value is not present in the vector then it returns the end iterator.
#include<bits/stdc++.h>
using namespace std;
int main(){
int n;cin>>n;
vector<int>v;
for(int i=0;i<n;i++){
cin>>v[i];
}
sort(v.begin(),v.end());
//lower bound for vector
auto pointer1 = lower_bound(v.begin(), v.end(), 7);
cout<<(*pointer1)<<endl;
//lower bound for array;
int array[n];
for(int i=0;i<n;i++){
cin>>array[i];
}
sort(array,array+n);
//lowerbound
int *pointer2 = lower_bound(array, array+n, 7);
//if you want upper bound then just replace lower_bound with upper_bound
}
The lower_bound() method in C++ is used to return an iterator pointing to the first element in the range [first, last) which has a value not less than val.
Code Example |
---|
Cpp :: max stack |
Cpp :: how to run a cpp file in visual studio |
Cpp :: c++ projects |
Cpp :: c++ find string in string |
Cpp :: is palindrom |
Cpp :: combination sum iv leetcode |
Cpp :: cpp map contains |
Cpp :: constants in cpp |
Cpp :: c++ cin string |
Cpp :: who made c++ |
Cpp :: how to replace an element in array in c++ |
Cpp :: aliasing c++ |
Cpp :: how to define range of numbers inside a if condition in c++ |
Cpp :: sinonimo de tratar |
C :: trie tableau c |
C :: java.lang.SecurityException: Permission denied (missing INTERNET permission?) |
C :: factorial in c |
C :: c program hide console window |
C :: how to print boolean in c |
C :: data types in c |
C :: addition of two matrix in c |
C :: best sites for loop practice c |
C :: c round function |
C :: how to turn off zsh |
C :: malloc in c |
C :: go optional parameters |
C :: int_min in c |
C :: double array in c |
C :: c read file content |
C :: c code to grade marks |