4

次のようなクラス(STLのコンテナの一部を模倣する)がある場合:


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_;

};

「cont_」内のすべての要素に対して prepare() を呼び出したい場合は、次の関数を作成できます。


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

}

私の質問は、「cont_」のすべての要素に対して Selector::prepare_elem() を呼び出したい場合はどうすればよいですか? 私の最初のアプローチはコンパイルされません:


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

}

2 番目のアプローチも失敗しました。


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

std::for_each() を使用して Selector::prepare_elem() を呼び出す方法はありますか?

方法があれば、ブーストなしの解決策を知りたいです。

4

1 に答える 1

1

boost::bindを使用したくない場合-std::tr1::bindを使用します。

于 2010-10-18T06:55:48.437 に答える