简体   繁体   English

用于std :: copy的自定义插入器

[英]Custom inserter for std::copy

Given a std::vector which holds objects of MyClass. 给定一个std::vector来保存MyClass的对象。 How can I create another vector which holds just data of one member of MyClass using std::copy ? 如何使用std::copy创建另一个只保存MyClass成员数据的向量? I guess I would have to implement a custom back_inserter but I could not figure out how to do this so far. 我想我必须实现一个自定义的back_inserter但到目前为止我无法弄清楚如何做到这一点。

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;

Use std::transform for that. 使用std::transform

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

(If you can't use C++11, you could make a function object yourself: (如果你不能使用C ++ 11,你可以自己创建一个函数对象:

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: 或者如果你可以使用TR1,请使用std::tr1::bind

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

BTW, as @Nawaz commented below, do a .reserve() to prevent unnecessary reallocation during the copy. BTW,正如@Nawaz在下面评论的那样,做一个.reserve()来防止在复制过程中不必要的重新分配。

vec2.reserve(vec1.size());
std::transform(...);

You want to use std::transform not std::copy and std::bind to bind to a pointer to a member variable: 您希望使用std::transform而不是std::copystd::bind来绑定到成员变量的指针:

#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 the handy vector initalization and use boost::bind instead this works just as well without C++11. 我的例子是C ++ 11,但是如果你跳过方便的矢量初始化并使用boost::bind ,那么没有C ++ 11也能正常工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM