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

STL algorithms taking the whole container rather than .begin(), end() as arg?(STL 算法将整个容器而不是 .begin(), end() 作为 arg?)

本文介绍了STL 算法将整个容器而不是 .begin(), end() 作为 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?

推荐答案

有一个不错的博文 由 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?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

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