I was curious so I trolled google a little bit, and chanced upon a solution to your problem:
namespace { std::string toString( const std::pair< size_t, size_t >& data) { std::ostringstream str; str << data.first << ", " << data.second; return str.str(); } } // namespace anonymous std::transform( some_map.begin(), some_map.end(), std::ostream_iterator< std::string >( std::cout, "\n" ), toString );
Just tried your solution and it works...Thanks everyone.
#include <iostream>
#include <string>
#include <map>
#include <iterator>
#include <sstream>
#include <algorithm>
namespace
{
std::string toString( const std::pair< std::string, unsigned long >& data)
{
std::ostringstream str;
str << data.first << ", " << data.second;
return str.str();
}
}
int main(int argc, char**argv)
{
std::map<std::string, unsigned long> the_words;
for (int i = 1; i < argc; ++i)
++the_words[argv[i]];
std::transform( the_words.begin(), the_words.end(), std::ostream_iterator< std::string >( std::cout, "\n" ),toString );
return 0;
}
usage/output
./testit this is the test
is, 1
test, 1
the, 1
this, 1