Binary search after sorting : binary_search « STL Algorithms Binary search « C++ Tutorial






#include <iostream>
#include <algorithm>
#include <vector>

using namespace std;

bool greater10( int value );

int main()
{
   const int SIZE = 10;
   int a[ SIZE ] = { 10, 2, 17, 5, 16, 8, 13, 11, 20, 7 };
   vector< int > v( a, a + SIZE );
   
   vector< int >::iterator location;
   location = find( v.begin(), v.end(), 16 );

   sort( v.begin(), v.end() );

   if ( binary_search( v.begin(), v.end(), 13 ) )
      cout << "\n\n13 was found in v";
   else
      cout << "\n\n13 was not found in v";

   return 0;
}

bool greater10( int value ) { return value > 10; }








26.1.binary_search
26.1.1.Illustrating the generic binary search algorithms
26.1.2.Use binary_search to locate a value in a vector
26.1.3.binary_search a list
26.1.4.Use binary_search() to binary search a vector
26.1.5.Binary search after sorting