1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51
|
#include <iostream>
#include <string>
#include <bobcat/binarysearch>
using namespace std;
string words[] =
{
"eight", // alphabetically sorted number-names
"five",
"four",
"nine",
"one",
"seven",
"six",
"ten",
"three",
"two"
};
bool compFun(string const &left, string const &right)
{
return left < right;
}
int main()
{
string *ret = FBB::binary_search(words, words + 10, "five");
if (ret != words + 10)
cout << "five is at offset " << (ret - words) << endl;
ret = FBB::binary_search(words, words + 10, "grandpa");
if (ret == words + 10)
cout << "grandpa is not the name of a number\n";
ret = FBB::binary_search(words, words + 10, "five",
[&](string const &element, string const &value)
{
return element < value;
}
);
if (ret != words + 10)
cout << "five is at offset " << (ret - words) << endl;
ret = FBB::binary_search(words, words + 10, "grandpa", compFun);
// or use: Comparator()
if (ret == words + 10)
cout << "grandpa is not the name of a number\n";
}
|