Custom installer for std :: copy

Given std::vector , which contains MyClass objects. How to create another vector that contains only the data of one member of MyClass using std::copy ? I assume that I would have to implement a custom back_inserter , but I could not figure out how to do this until now.

 struct MyClass { int a; } std::vector<MyClass> vec1; // I could copy that to another vector of type MyClass using std::copy. std::copy(vec1.begin(), vec1.end(); std::back_inserter(someOtherVec) // However I want just the data of the member a, how can I do that using std::copy? std::vector<int> vec2; 
+7
source share
2 answers

Use std::transform for this.

 std::transform(vec1.begin(), vec1.end(), std::back_inserter(vec2), [](const MyClass& cls) { return cls.a; }); 

(If you cannot use C ++ 11, you can create a function object yourself:

 struct AGetter { int operator()(const MyClass& cls) const { return cls.a; } }; std::transform(vec1.begin(), vec1.end(), std::back_inserter(vec2), AGetter()); 

or use std::tr1::bind if you can use TR1:

 std::transform(vec1.begin(), vec1.end(), std::back_inserter(vec2), std::tr1::bind(&MyClass::a, std::tr1::placeholders::_1)); 

BTW, as @Nawaz commented below, execute .reserve() to prevent unnecessary redistribution during copying.

 vec2.reserve(vec1.size()); std::transform(...); 
+15
source

You want to use std::transform not std::copy and std::bind to bind to a pointer to a member variable:

 #include <algorithm> #include <iterator> #include <vector> #include <iostream> #include <functional> struct foo { int a; }; int main() { const std::vector<foo> f = {{0},{1},{2}}; std::vector<int> out; out.reserve(f.size()); std::transform(f.begin(), f.end(), std::back_inserter(out), std::bind(&foo::a, std::placeholders::_1)); // Print to prove it worked: std::copy(out.begin(), out.end(), std::ostream_iterator<int>(std::cout, "\n")); } 

My example is C ++ 11, but if you skip convenient vector initialization and use boost::bind , then this will work just as well as C ++ 11.

+4
source

All Articles