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

Popular posts from this blog

javascript - How to synchronize the Three.js and HTML/SVG coordinate systems (especially w.r.t. the y-axis)? -

javascript - How do I find how many occurences are there of a highlighted string, and which occurence is it? -

java - Reading data from multiple zip files and combining them to one -