Mega Code Archive

 
Categories / C++ Tutorial / STL Algorithms Binary Search
 

Binary search after sorting

#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; }