STL 算法将整个容器而不是 .begin(), end() 作为 arg?

2022-01-24 00:00:00 containers iterator c++ c++11 stl

独立的 STL 算法(如 std::count_if)采用一对迭代器.在我使用这些的所有情况下(以及我在网上看到的所有示例!),我发现自己在输入

Stand-alone STL algorithms (like std::count_if) take pair of iterators. In all cases where I use those (and in all examples I've seen online!), I find myself typing

std::count_if(myContainer.begin(),myContainer.end(), /* ... */ );

样式的速记模板有什么原因吗

Is there a reason why shorthand templates of the style

std::count_if(myContainer, /* ... */ );

没有提供,因为更多的是对整个容器执行的操作?我只是忽略了吗?c++11和c++03的答案不同吗?

are not provided, given that more of than not is the operaation performed on the whole container? Did I just overlook it? Is the answer different for c++11 and c++03?

推荐答案

有一个不错的博文 由 Herb Sutter 讨论该问题.要点是,如果已经存在具有相同数量模板参数的算法的重载,则为算法添加基于容器的重载可能会产生歧义.概念旨在解决这个问题.

There is a nice blog-post by Herb Sutter discussing the question. The gist is that adding container-based overloads for algorithms can create ambiguities if an overload for that algorithm with the same number of template-parameters already exists. Concepts were intended to fix that problem.

相关文章