c++ - How to use lower_bound(upper_bound) to find position of any number in array? -


for example, have array elements 1,7,9,23,34,47,67,89,123,234,345,567. need know position of 123.

declare , initialize array elements. create vector of ints. use upper_bound() on vector. here example:

#include<iostream> #include<algorithm> #include<vector> using namespace std;  int main() {     int arr[] = {1,7,9,23,34,47,67,89,123,234,345,567};     int len = sizeof(arr)/sizeof(arr[0]);     vector<int> v(arr,arr+len);      vector<int>::iterator upper;     upper = upper_bound(v.begin(), v.end(), 123);     cout<<(upper-v.begin())<<endl;  // output: 9      return 0; } 

hope helps!!


Comments

Popular posts from this blog

account - Script error login visual studio DefaultLogin_PCore.js -

xcode - CocoaPod Storyboard error: -