Thanks for your input. However, using std::for_each complicates things
code and not to read the function object.
irek.szczesniak@gmail.com writes:
Hi,
I would like to do something like this, which is the WISH case:
typedef map<int, double> hash;
hash a;
// ...
FOREACH(a, key, value, hash)
std::cout << key << "->" << value << "\n";
Would it be easier to use for_each()?
Something like this:
#########
#include <iostream>
#include <map>
#include <cstdlib>
struct PrintOut
{
private:
int key_;
public:
PrintOut( int key ) : key_(key) {}
void operator() ( const std::pair<int, double>& p )
{
if (key_ == p.first)
std::cout << p.first << "->" << p.second << "\n";
}
};
int
main()
{
std::map<int, double> a;
a.insert( std::make_pair<int,double>(0, 1.71) );
a.insert( std::make_pair<int,double>(1, 3.14) );
std::for_each( a.begin(), a.end(), PrintOut(1) );
return EXIT_SUCCESS;
}
#########
--
John L. Fjellstad
web: http://www.fjellstad.org/ Quis custodiet ipsos custodes
Replace YEAR with current four digit year
[ comp.lang.c++.moderated. First time posters: Do this! ]