c++ - Copying a vector of pointers -
i have std::vector<a*> need deep copy vector using a::clone(). 
instead of using handwritten loops, wondering whether use for_each or standard library algorithm this.
the appropriate algorithm std::transform , can turn member function invocation unary functor std::mem_fun
example:
#include <vector> #include <functional> #include <algorithm> #include <iterator>  class x { public:     x* clone(); };  int main() {     std::vector<x*> vec1, vec2;     std::transform(vec1.begin(), vec1.end(), std::back_inserter(vec2), std::mem_fun(&x::clone)); } if target vector same size input range, can pass vec2.begin() third argument. use back_inserter if target empty (or want append it).
Comments
Post a Comment