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 52 53 54
|
#include <iostream>
#include <string>
#include <algorithm>
#include <cstring>
const char* words[] =
{
"zero",
"one",
"two",
"three",
"four",
"five",
"six",
"six",
"six",
"seven",
"eight",
"nine",
"ten"
};
template <typename T>
void print( T begin, T end )
{
auto cur = begin ;
while ( cur != end )
std::cout << '\t' << *cur++ << '\n' ;
}
int main()
{
std::sort(std::begin(words), std::end(words),
[](const char*a, const char*b){ return std::strcmp(a,b) < 0 ; }) ;
print(std::begin(words), std::end(words)) ;
std::string input ;
while ( std::cout << "Enter a word: " && std::cin >> input && input != "quit" )
{
auto it = std::lower_bound(std::begin(words), std::end(words), input) ;
std::cout << "Words before '" << input << "':\n" ;
print(std::begin(words), it) ;
it = std::upper_bound(std::begin(words), std::end(words), input) ;
std::cout << "Words after '" << input << "'\n" ;
print(it, std::end(words)) ;
auto range = std::equal_range(std::begin(words), std::end(words), input,
[](const std::string&a, const std::string&b){return a<b;}) ;
std::cout << '\'' << input << "' occurs " << range.second - range.first << " times.\n" ;
}
}
|