_GENERIC PROGRAMMING AND THE C++ STL_ by Dan Zigmond Listing One #include #include #include #include // Return a copy of the string in "standard" form (lowercase, no punctuation) string standardize( string s ) { string::iterator i = remove_if( s.begin(), s.end(), ispunct ); s.erase( i, s.end() ); transform( s.begin(), s.end(), s.begin(), tolower ); return s; } // Filter a text file into an alphabetzied list of unique words contained // in that file, ignoring case and punctuation. int main( int argc, char** ) { if ( argc != 1 ) throw("usage: lexicon\n"); set< string, less< string > > words; transform( istream_iterator< string, ptrdiff_t >( cin ), istream_iterator< string, ptrdiff_t >(), inserter( words, words.end() ), standardize ); copy( words.begin(), words.end(), ostream_iterator< string >( cout, "\n" ) ); return( 0 ); } Listing Two #include #include #include #include // Return a copy of the string in "standard" form (lowercase, no punctuation) string standardize( string s ) { string::iterator i = remove_if( s.begin(), s.end(), ispunct ); s.erase( i, s.end() ); transform( s.begin(), s.end(), s.begin(), tolower ); return s; } // Filter a text file into an alphabetzied list of unique words contained // in that file, ignoring case and punctuation. int main( int argc, char** ) { if ( argc != 1 ) throw("usage: lexicon2\n"); vector< string > words; transform( istream_iterator< string, ptrdiff_t >( cin ), istream_iterator< string, ptrdiff_t >(), inserter( words, words.end() ), standardize ); sort( words.begin(), words.end() ); vector< string >::iterator i = unique( words.begin(), words.end() ); copy( words.begin(), i, ostream_iterator< string >( cout, "\n" ) ); return( 0 ); } Example 1: (a) int numbers[ 100 ]; .... int* i = find( numbers, numbers + 100, 37 ); (b) list< int > numbers; .... list< int >::iterator i = find( numbers.begin(), numbers.end(), 37 ); Example 2: vector< string > words; copy( istream_iterator< string, ptrdiff_t >( cin ), istream_iterator< string, ptrdiff_t >(), inserter( words, words.end() ) ); Example 3: (a) transform( words.begin(), words.end(), words.begin(), standardize ); (b) transform( istream_iterator< string, ptrdiff_t >( cin ), istream_iterator< string, ptrdiff_t >(), inserter( words, words.end() ), standardize ); (c) sort( words.begin(), words.end() ); vector< string >::iterator i = unique( words.begin(), words.end() ); copy( words.begin(), i, ostream_iterator< string >( cout, "\n" ) ); Example 4: string standardize( string s ) { string:iterator i = remove_if( s.begin(), s.end(), ispunct ); s.erase( i, s.end() ); transform( s.begin(), s.end(), s.begin(), tolower ); return s; } Example 5: (a) set< string, less< string > > words; transform( istream_iterator< string, ptrdiff_t >( cin ), istream_iterator< string, ptrdiff_t >(), inserter( words, words.end() ), standardize ); (b) copy( words.begin(), words.end(), ostream_iterator< string >( cout, "\n" ) );