当我尝试使用 for_each
就地更改 vector 时:
vector<bool> sub_accs_ind(vec_ids_.size());
std::for_each(sub_accs_ind.begin(), sub_accs_ind.end(), [](bool& b){ b = false; });
它导致错误 /usr/include/c++/4.8/bits/STL_algo.h:4417:14: error: no match for call to '(main(int, char * const*)::__lambda3) (std::_Bit_iterator::reference)'
__f(*__first);
你能告诉我这里有什么问题吗?
最佳答案
std::vector<bool>
is not a container !
它的迭代器不返回 bool&
,而是一个代理实例。在 C++11 中,您必须显式命名其类型:
std::for_each(
sub_accs_ind.begin(),
sub_accs_ind.end(),
[](decltype(sub_accs_ind)::reference b){ b = false; }
);
C++14 允许您将参数声明为 auto&&
以适应真实引用和代理。
关于c++ - for_each 用 vector<bool> 的 lambda 就地修改,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38585826/