Boost logo

Boost Users :

From: Darren Cook (darren_at_[hidden])
Date: 2003-08-25 06:05:05


Hi,
I have a project with many occurences of while loops iterating through a
container, and operating on the data. I'd like to use for_each() and turn
them into one-liners, but I don't like writing one-off functor classes.

I was hoping boost::bind could help, but I've read the docs, scratched my
head, and failed to work out how the examples apply to my case. Maybe they
don't and I'm trying to use it for something it cannot do?

See an example below. My function currently looks something like grab_keys()
and I want it to look like grab_keys2().

Could some kind soul show me how to write this? (or explain why it cannot be
written).

Thanks,

Darren

-------------------------

class Info{
public:
int key;
std::set<int> points;
};

class InfoContainer{
public:
Info* get(int);
};

void grab_keys(const InfoContainer &cs,const Info *c){
std::set<int> list;
std::set<int>::const_iterator i=c->points.begin();
while(i!=c->points.end()){
        Info *ec=cs.get(*i++);
        list.add(ec->key);
        //Previous two lines could be written: list.add(cs.get(*i++)->key);
        }
//Do something with list here.
}

void grab_keys2(const AllInfo &cs,const Info *c){
Points list;
std::for_each(c->points.begin(),c->points.end(),
      boost::bind( /* what goes here? */ )
      );
//Do something with list here.
}


Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net