C++ for_each(for_each())?
这是可行的,因为每个通过向量C++ for_each(for_each())?,c++,c++11,vector,foreach,C++,C++11,Vector,Foreach,这是可行的,因为每个通过向量 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); 和函数仅用于整数 void def(const int& i) { std::cout << i; } void def(const int&i){st
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);
和函数仅用于整数
void def(const int& i) { std::cout << i; }
void def(const int&i){std::cout最简单的解决方案是在lambda中为每个
传递:
std::for_each(vvit, vv.end(), [f](std::vector<int> const& v)
{ std::for_each(v.begin(), v.end(), f); } );
你的问题是什么?你想知道为什么这不起作用吗?为什么你希望它会起作用?对不起,是的,为什么for_不接受内部for_作为函数参数,外部应用于外部向量,内部应用于[each]内部向量?所以它很简单,为什么?在反思时,它应该是“为什么def函数的应用方式不使其参数与向量的int匹配”?for_each
接受一个以元素作为其第三个参数的函数。for_each(b,e,f)
是一个类型为void
的表达式。因此,它不是每个的的第三个参数的有效参数。我明白了,我想我需要听到这个。我会接受作为答案-谢谢
void def(const int& i) { std::cout << i; }
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);
}
}