member container and for_each with mem_fun

C

cinsk

Hi,

If I have a class (that mimic some of STL's container) like this:

class Elem {
public:
void prepare(); // do something on *this
// ...
};

class Selector {
public:
typedef vector<Elem *> container_type;
typedef container_type::iterator iterator;

iterator begin() { return cont_.begin(); }
iterator end() { return cont_.end(); }

void check_all();

private:
prepare_elem(Elem *p); // do something on 'p'
container_type cont_;
};

If I want to call prepare() for all elements in 'cont_', I could make
the following function:

void Selector::check_all() {
for_each(cont_.begin(), cont_.end(), mem_fun(&Elem::prepare));
}

My question is, what if I want to call Selector::prepare_elem() for
all elements in 'cont_'? My initial approach won't compile:

void Selector::check_all() {
for_each(cont_.begin(), cont_.end(),
mem_fun(&Selector::prepare_elem));
}

Is there anyway to use std::for_each() to call
Selector::prepare_elem()?

Thanks in advance.
 
C

cinsk

Hi,

If I have a class (that mimic some of STL's container) like this:

class Elem {
public:
  void prepare(); // do something on *this
  // ...

};

class Selector {
public:
  typedef vector<Elem *> container_type;
  typedef container_type::iterator iterator;

  iterator begin() { return cont_.begin(); }
  iterator end() { return cont_.end(); }

  void check_all();

private:
  prepare_elem(Elem *p); // do something on 'p'
  container_type cont_;

};

If I want to call prepare() for all elements in 'cont_', I could make
the following function:

void Selector::check_all() {
  for_each(cont_.begin(), cont_.end(), mem_fun(&Elem::prepare));

}

My question is, what if I want to call Selector::prepare_elem() for
all elements in 'cont_'?  My initial approach won't compile:

void Selector::check_all() {
  for_each(cont_.begin(), cont_.end(),
mem_fun(&Selector::prepare_elem));

}

Is there anyway to use std::for_each() to call
Selector::prepare_elem()?

Thanks in advance.

If possible, I would like to know a solution using standard way
(except boost).
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top