STL algorithms taking the whole container rather than .begin(), end() as 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?
基础教程推荐
- 分别使用%o和%x以八进制或十六进制格式显示整 1970-01-01
- C++按值调用 1970-01-01
- C++ #define 1970-01-01
- C++输入/输出运算符重载 1970-01-01
- C语言访问数组元素 1970-01-01
- C++定义类对象 1970-01-01
- 初始化变量和赋值运算符 1970-01-01
- end() 能否成为 stl 容器的昂贵操作 2022-10-23
- 明确指定任何或所有枚举数的整数值 1970-01-01
- 使用scanf()读取字符串 1970-01-01