C Lower Bound Example - C

What is lower bound in C?

Snippet Code


  
Rate this page :
  [ 0 votes]

Lower bound in c is generally looks like doing a binary search, but

(a) If your required element is not found then you will return to your current place in the search.
(b) In case if your required element is found , you may search on until you find an non matching content. After that you return a pointer to the first matching element.
In Cpp, lower_bound concept is easy to understand.Here,the example code for lower_bound in Cpp.

#include <iostream> #include <algorithm> #include <vector> int main () { int arr[] = {10,20,30,30,20,10,10,20}; std::vector<int> v(arr,arr 8); // 10 20 30 30 20 10 10 20 std::sort (v.begin(), v.end()); // 10 10 10 20 20 20 30 30 std::vector<int>::iterator low,up; low=std::lower_bound (v.begin(), v.end(), 20); up= std::upper_bound (v.begin(), v.end(), 20); std::cout << "lower_bound at position " << (low- v.begin()) << 'n'; std::cout << "upper_bound at position " << (up - v.begin()) << 'n'; return 0; }

Tags


Ask Questions

Ask Question