STL算法取整个容器而不是.begin(),end()作为arg? [英] STL algorithms taking the whole container rather than .begin(), end() as arg?
问题描述
独立的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?
推荐答案
有一个不错的 blog-post by 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.
这篇关于STL算法取整个容器而不是.begin(),end()作为arg?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!