// Driver for SortedList // Author: U.Breymann / H.P.Weber // Date: 05.05.05 #include using std::cout; using std::endl; #include using std::setw; #include // std::copy #include // sin() #include // abs(int) #include "SortedList.h" template <> class Compare< int > { // specialization public: enum sortCriterion{ increasing, decreasing, absoluteValue }; Compare( sortCriterion s = increasing ) : howToSort( s ) { } // Constructor bool operator()( const int& a, const int& b ) const { bool result; switch( howToSort ) { case increasing: result = a < b; break; case decreasing: result = a > b; break; case absoluteValue: result = abs(a) < abs(b); break; default: throw "Compare.operator()(): default darf nicht erreicht werden"; } return result; } private: sortCriterion howToSort; }; int main() { Compare< int > absolute( Compare< int >::absoluteValue ); SortedList< int > sortedList, sortedListAbsolute( absolute ); std::ostream_iterator< int > output( cout, "\t" ); cout << "Werte mit verschiedenen Vorzeichen einfuegen:\n"; for( int i = 0; i < 7; ++i ) { int value = int( 100.0 * sin( static_cast< float >( i ) ) ); cout << *sortedList.insert( value ) << endl; sortedListAbsolute.insert( value ); } cout << "\nincreasing sortiert( default ):\n"; std::copy( sortedList.begin(), sortedList.end(), output ); cout << "\n\nnach Absolutbetrag sortiert:\n"; std::copy( sortedListAbsolute.begin(), sortedListAbsolute.end(), output ); cout << "\n\nWert 90 finden" << endl; SortedList< int >::Iterator search = sortedList.find( 90 ); if( search == sortedList.end() ) cout << "nicht gefunden\n"; else cout << *search << " gefunden" << endl; cout << "\nWert 91 finden" << endl; search = sortedList.find( 91 ); if( search == sortedList.end() ) cout << "nicht gefunden\n\n"; else cout << *search << " gefunden" << endl << endl; return 0; }