Here can be a one more example for you to understand more deeply about the upper_bound() function of the C++ language :
Code:
#pragma warning(disable: 4786)
#include <iostream>
#include <algorithm>
#include <functional>
#include <vector>
using namespace std;
int main()
{
const int VECTOR_SZ = 8 ;
typedef vector<int > IVector ;
typedef IVector::iterator IVectort ;
IVector Nmbrs(VECTOR_SZ) ;
IVectort st, nd, i, lctn ;
Nmbrs[0] = 4 ;
Nmbrs[1] = 10;
Nmbrs[2] = 10 ;
Nmbrs[3] = 30 ;
Nmbrs[4] = 69 ;
Nmbrs[5] = 70 ;
Nmbrs[6] = 96 ;
Nmbrs[7] = 100;
st = Nmbrs.begin() ;
nd = Nmbrs.end() ;
cout << "Nmbrs { " ;
for(i = st; i != nd; i++)
cout << *i << " " ;
cout << " }\n" << endl ;
lctn = upper_bound(st, nd, 10) ;
cout << "First location element 10 can be inserted in Nmbrs is: "
<< lctn - st << endl ;
}
Output
Code:
Nmbrs { 4 10 10 30 69 70 96 100 }
First location element 10 can be inserted in Nmbrs is: 1
Bookmarks