迭代向量是可行的:
std::vector<int> collection = {2, 3, 4, 5435345, 2};
std::for_each(collection.begin(), collection.end(), [](int& i){cout << i << endl;});
但不能超过一个集合(编译错误):
std::set<int> collection = {2, 3, 4, 5435345, 2};
std::for_each(collection.begin(), collection.end(), [](int& i){cout << i << endl;});
为什么我无法使用std :: for_each
迭代std :: set
?
奖励问题:
此外,我想将lambda的参数中的int&
更改为auto&
,为什么不能自动推导出它的类型?