Mega Code Archive

 
Categories / C++ Tutorial / STL Algorithms Binary Search
 

Use equal_range to locate the first and last point at which 5 can be inserted in order

#include <iostream> #include <algorithm> #include <vector> using namespace std; int main() {    const int SIZE = 10;    int a1[] = { 2, 2, 4, 4, 4, 6, 6, 6, 6, 8 };    vector< int > v( a1, a1 + SIZE );    pair< vector< int >::iterator, vector< int >::iterator > eq;    cout << "\n\nUse equal_range to locate the first and last point at which 5 can be inserted in order";    eq = equal_range( v.begin(), v.end(), 5 );    cout << "\n   Lower bound of 5 is element "         << ( eq.first - v.begin() ) << " of vector v";    cout << "\n   Upper bound of 5 is element "         << ( eq.second - v.begin() ) << " of vector v"          << endl;                return 0; }