这是可行的,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);函数f,它将for_each重新应用于内部向量ints中。
void f(const std::vector<int>& v) {std::for_each(v.begin(), v.end(), def);}但for_each在for_each内
std::for_each(vvit, vv.end(), std::for_each((*vvit).begin(), (*vvit).end(), def));以及仅用于ints的函数
void def(const int& i) { std::cout << i; }不会的。(如果我尝试正确的话,也不使用bind。)编译器说def函数不能应用正确的转换,即从向量分配器(向量的位置指针?)对于const &,前面的例子用向量分离函数f实现了这一点.
这是复杂还是琐碎?
发布于 2013-11-14 01:03:03
最简单的解决方案是在lambda中传递for_each:
std::for_each(vvit, vv.end(), [f](std::vector<int> const& v)
{ std::for_each(v.begin(), v.end(), f); } );但有什么问题吗
for (auto const& v : vv) {
for (int i : v) {
f(i);
}
}https://stackoverflow.com/questions/19967493
复制相似问题