如何使用(lambda)函数填充C ++容器?

时间:2018-07-12 13:38:38

标签: c++ stl containers

我想用指向对象的指针初始化一个容器。我目前有这样一个循环:

for(int i=0;i < n;i++) {
    container.push_back(new Object());
}

哪些C ++操作(即类似于std::transform)有权替换此循环并使用n新创建的对象初始化容器?

3 个答案:

答案 0 :(得分:7)

使用std::generate

constexpr int n = 10;
std::vector<Object*> v1(n);
std::generate(v1.begin(), v1.end(), [](){ return new Object(); });

std::generate_n

std::vector<Object*> v2;
v2.reserve(n); // pre-allocate sufficient memory to prevent re-allocations
               // (you should have done in original loop approach as well)
std::generate_n(std::back_inserter(v2), n, [] { return new Object(); });

答案 1 :(得分:5)

您可以将std::generate_nstd::back_inserter与lambda一起使用。

std::generate_n(std::back_inserter(container), n, [] { return new Object(); });

答案 2 :(得分:0)

目标是这种语法:

std::vector<Object*> v1 = generate([](auto&&){ return new Object; }, 10).make_container();

我们说要生成具有特定lambda的10个元素,然后创建一个要求类型的容器。

它需要一些样板。首先,一个计数并调用函数的输入迭代器:

template<class F>
struct generator_iterator {
  F f;
  std::size_t i = 0;

  using self=generator_iterator;
  friend bool operator==(self const& lhs, self const& rhs){ return lhs.i==rhs.i; }
  friend bool operator!=(self const& lhs, self const& rhs){ return lhs.i!=rhs.i; }
  using reference=std::result_of_t<F const&(std::size_t const&)>;
  using value_type=std::decay_t<reference>;
  using difference_type=std::ptrdiff_t;
  using pointer=value_type*;
  using iterator_category=std::input_iterator_tag;

  self& operator++(){++i; return *this;}
  self operator++(int){auto tmp=*this; ++*this; return tmp;}
  reference operator*()const{ return f(i); }
  pointer operator->()const { return std::addressof(f(i)); }

  friend difference_type operator-( self const& lhs, self const& rhs ) { return lhs.i-rhs.i; }
  self& operator-=( difference_type rhs )& {
    i-=rhs;
    return *this;
  }
  self& operator+=( difference_type rhs )& {
    i+=rhs;
    return *this;
  }
  friend difference_type operator+( self lhs, difference_type rhs ) {
    lhs += rhs;
    return lhs;
  }
  friend difference_type operator-( self lhs, difference_type rhs ) {
    lhs -= rhs;
    return lhs;
  }

};

接下来是一个范围原语,它带有.make_container()方法,可通过显式或隐式传递类型将范围转换为容器:

template<class It>
struct range_t {
  It b, e;
  It begin() const { return b; }
  It end() const { return e; }

private:
  struct container_maker {
    range_t const* self;
    template<class C>
    operator C()&& {
      return {self->begin(), self->end()};
    }
  };
public:
  container_maker make_container()const{
    return {this};
  }
  // C is optional
  template<class C>
  C make_container()const{
    return make_container();
  }
};
template<class It>
range_t<It> range( It s, It f ) {
  return {std::move(s), std::move(f)};
}

然后我们将它们粘合在一起:

template<class F>
auto generate( F&& f, std::size_t count ) {
  generator_iterator<std::decay_t<F>> e{f, count};
  generator_iterator<std::decay_t<F>> b{std::forward<F>(f)};
  return range( std::move(b), std::move(e) );
}

然后编译:

std::vector<Object*> v1 = generate([](auto&&){ return new Object; }, 10).make_container();

Live example