#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <cctype>
#include <fstream>
struct ci_char_traits : public std::char_traits< char >
{
static
bool eq( char c1, char c2 )
{
return std::tolower( c1 ) == std::tolower( c2 );
}
static
bool ne(char c1, char c2)
{
return std::tolower( c1 ) != std::tolower( c2 );
}
static
bool lt(char c1, char c2)
{
return std::tolower( c1 ) < std::tolower( c2 );
}
static
int compare( const char* s1, const char* s2, size_t n )
{
for( ;*s1 && *s2; ++s1, ++s2 )
{
int diff = std::tolower( *s1 ) - std::tolower( *s2 );
if( diff )
return diff;
}
return ( *s1 != '\0' ) - ( *s2 != '\0' );
}
static
const char* find( const char* s, int n, char a )
{
while( n-- > 0 && std::tolower( *s ) != std::tolower( a ) )
{
++s;
}
return n >= 0 ? s : 0;
}
};
typedef std::basic_string< char, ci_char_traits > ci_string;
typedef std::vector< ci_string > s_string;
void print_data( s_string& con )
{
for( s_string::const_iterator it = con.begin( ); it != con.end( ); ++it )
{
std
::cout << (*it
).
c_str() << "\n"; }
}
void eat_endline( std::string &str )
{
std::size_t len_of = str.length( );
if( len_of > 0 && str[ len_of - 1 ] == '\n' )
str = str.substr( 0, len_of - 1 );
}
int main( )
{
s_string database;
std::fstream client_file;
char advance;
std::string name;
s_string::iterator pos;
client_file.open("abc.txt");
while( std::getline( client_file, name ) )
{
eat_endline( name );
database.push_back( name.c_str( ) );
}
std::sort( database.begin(), database.end( ) );
print_data( database );
do
{
std
::cout << "Enter search string: " << std
::flush; std::string item;
std
::getline( std
::cin, item
); eat_endline( item );
if( std::binary_search( database.begin( ), database.end( ), item.c_str( ) ) )
{
std
::cout << "Item found : " << item.
c_str( ) << "\n"; }
else
{
std
::cout << "Item not found .\n"; print_data( database );
}
std
::cout << "Continue (y, n) ? : "; std
::cin.
ignore( 80,
'\n' ); }
while( advance == 'y' || advance == 'Y' );
return 0;
}