我刚刚遇到这样一个事实,即标准algorithm
标头中的几种算法不需要std::
。是否有任何原因find_if,for_each,计数等不需要std ::?
例子:
#include <vector>
#include <algorithm>
int main() {
std::vector<int> m;
count(m.begin(), m.end(), 0);
count_if(m.begin(), m.end(), [](auto){return true;});
for_each(m.begin(), m.end(), [](auto){});
find_if(m.begin(), m.end(), [](auto){return true;});
}
是否有任何具体的原因是什么? g++
和clang++
都接受上面的代码。
由于ADL,在你重构使用自定义容器的情况下,最好不要依赖它。 –
https://en.wikipedia.org/wiki/Argument-dependent_name_lookup –