Demonstrate binary_search() in vector
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
int main()
{
vector<char> vectorObject;
bool result;
int i;
for(i = 0; i <10; i++)
vectorObject.push_back('A' + i);
cout << "Contents of vectorObject: ";
for(i = 0; i <vectorObject.size(); i++)
cout << vectorObject[ i ];
cout << endl;
cout << "Looking for F.";
result = binary_search(vectorObject.begin(), vectorObject.end(), 'F');
if(result)
cout << "F Found";
else
cout << "F Not Found";
cout << "Looking for X.\n";
result = binary_search(vectorObject.begin(), vectorObject.end(), 'X');
if(result)
cout << "X Found\n";
else
cout << "X Not Found\n";
return 0;
}
Related examples in the same category