STL算法:为什么没有额外的容器接口(除了迭代器对)? [英] STL algorithms: Why no additional interface for containers (additional to iterator pairs)?
问题描述
我想知道为什么STL不会重载它们的算法函数,所以我可以通过简单地提供一个容器来调用它们,而不是采取更冗长的方式来传递begin + end迭代器。我当然明白为什么我们还想使用一个迭代器对来处理容器/数组的子序列,然而,几乎所有调用这些方法都使用一个完整的容器:
I'm wondering why the STL doesn't overload their algorithm functions such that I can call them by simply providing a container and not taking the more verbose way to pass begin + end iterators. I of course understand why we also want to use an iterator pair for processing subsequences of a container / array, however, almost all calls to these methods are using a whole container:
std::for_each(myVector.begin(), myVector.end(), doSomething);
我会发现它更方便,可读和可维护,只需写
I'd find it more convenient, readable and maintainable to just write
std::for_each(myVector, doSomething);
STL有没有理由不提供这些重载? 它们引入歧义吗?我在想这样的东西:
Is there a reason STL doesn't provide these overloads? Do they introduce ambiguity? I'm thinking about something like this:
template<typename _Container, typename _Funct>
inline _Funct for_each(_Container c, _Funct f) {
return for_each(begin(c), end(c), f);
}
我错过了什么?
推荐答案
他们做对许多算法引入歧义。很多< algorithm>
看起来像
They do introduce ambiguity for many algorithms. A lot of <algorithm>
looks like
template<class iterator>
void do_something(iterator, iterator);
template<class iterator, class funct>
void do_something(iterator, iterator, funct);
如果您添加额外的重载
template<class container, class funct>
void do_something(container, funct);
编译器会遇到一些麻烦,找出什么 do_something(x,y)
表示。 如果 *) x
和 y
是相同的类型
,它将匹配 iterator = type
和 container = type,funct = type
。
the compiler will have some trouble figuring out what do_something(x, y)
means. If *)x
and y
are of the same type
, it will match both iterator = type
and container = type, funct = type
.
C ++ 11尝试使用概念。但是,这些概念太复杂了,不能成为标准,所以也没有这些重载。
C++11 tried to solve this with "concepts" that could recognize the difference between a container and an iterator. However, these "concepts" turned out to be too complicated to make it into the standard, so neither did these overloads.
*) em>编译器在这里不同意,Comeau编译器声称它是不明确的,g ++ 4.5和MSVC 10调用第一个函数。
*) compilers disagree here, the Comeau compiler claims that it is ambiguous, g++ 4.5 and MSVC 10 calls the first function.
在注释中进行了一个非常长的讨论后,这里有一个例子,它不能按预期工作 - 使用一个也可以作为谓词的容器适配器。
After an extremely long discussion in the comments, here is one example where it doesn't work as expected - using a container adapter that can also double as a predicate.
#include <iostream>
#include <vector>
template<class iterator>
void test(iterator, iterator)
{
std::cout << "test iterator\n";
}
template<class iterator, class predicate>
void test(iterator, iterator, predicate)
{
std::cout << "test iterator, predicate\n";
}
template<class container, class predicate>
void test(const container& cont, predicate compare)
{
std::cout << "test container, predicate\n";
test(cont.begin(), cont.end(), compare);
}
template<class container>
class adapter
{
public:
typedef typename container::iterator iterator;
adapter(container* cont) : cont(cont)
{ }
iterator begin() const
{ return cont->begin(); }
iterator end() const
{ return cont->end(); }
bool operator()(const iterator& one, const iterator& two)
{ return *one < *two; }
private:
container* cont;
};
int main()
{
std::vector<int> v;
adapter<std::vector<int>> a(&v);
test(a, a);
}
输出:
测试迭代器
test iterator
这篇关于STL算法:为什么没有额外的容器接口(除了迭代器对)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!