简体   繁体   English

for_each(for_each())?

[英]for_each( for_each() )?

This works, for_each passes vectors 这有效,for_each传递向量

std::vector<int> v(10, 1);
std::vector< std::vector<int> > vv(10, v);
auto vvit = vv.begin();

std::for_each(vvit, vv.end(), f);

to function f which applies for_each anew to work with inner vector ints 函数f,它适用于for_each重新使用内部向量int

void f(const std::vector<int>& v) {std::for_each(v.begin(), v.end(), def);}

but for_each within for_each 但for_each中的for_each

std::for_each(vvit, vv.end(), std::for_each((*vvit).begin(), (*vvit).end(), def));

and function for just ints 和整数的功能

void def(const int& i) { std::cout << i; }

does not. 才不是。 (Nor with bind, if I tried correctly.) The compiler says that the def function cannot apply the right conversion ie from vector allocator (the vector's position pointer?) to const int&, something the former example achieves with the vector-separating function f. (如果我正确尝试的话,也没有绑定。)编译器说def函数不能应用正确的转换,即从向量分配器(向量的位置指针?)到const int&,前一个例子用向量分离函数f实现的东西。

Is this complicated or trivial? 这是复杂还是微不足道的?

Easiest solution is to pass for_each in a lambda: 最简单的解决方案是在lambda中传递for_each

std::for_each(vvit, vv.end(), [f](std::vector<int> const& v)
  { std::for_each(v.begin(), v.end(), f); } );

But what's wrong with 但是有什么问题

for (auto const& v : vv) {
  for (int i : v) {
    f(i);
  }
}

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

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