c++ - How to use std::mem_fn to call a method on all objects inside a vector? -
given vector such as:
vector<something*> a; i want call function whoami() on every something object. want append return value (string) ostream objects inside vector.
here's code:
std::transform(a.begin(), a.end(), std::ostream_iterator<std::string>(outstream_), std::mem_fn(&something::whoami))); edit: bad, forgot reference.
after adding missing & in argument mem_fn, your code works.
however, why use transform here in first place? proper solution problem is
for (auto s : a) outstream_ << s->whoami();
Comments
Post a Comment