Listing A
// this will work only for 'char' streams!!! class write_key_and_value { public: template< class CharType, class CharTraits, class Key, class Value> void operator() ( std::basic_ostream< CharType, CharTraits> & streamOut, const std::pair< const Key, Value> & value) { streamOut << "key: " << value.first << ", value: " << value.second; } }; std::map< std::string, int> collWords; collWords.insert( std::make_pair( (std::string)"easily", 3)); collWords.insert( std::make_pair( (std::string)"write", 5)); collWords.insert( std::make_pair( (std::string)"collections", 12)); std::cout << coll_container( collWords, formatter( write_key_and_value())); // will print out: // key: collections, value: 12 // key: easily, value: 3 // key: write, value: 5