为什么(必须)从std :: iterator继承?

0lt*_*0lt 2 c++ iterator

据我所知,迭代器是一种为客户端提供接口的机制,用于观察/迭代/传递例如自定义集合的内容,而不会破坏信息隐藏原则.STL容器有自己的迭代器,所以我们可以使用for ( : )for_each循环它们没有问题.

我的问题最初是:为什么要继承std::iterator?与以下示例相比,它提供了哪些附加功能:

SimpleArray.h

class SimpleArray
{
    int *arr;
    int n;
public:
    explicit SimpleArray(int = 1);
    ~SimpleArray();

    friend ostream& operator<<(ostream&, const SimpleArray&);
    friend istream& operator>>(istream&, SimpleArray&);

    // encapsulated "iterator"
    class Observer
    {
        int *p;
    public:
        Observer(int *value = nullptr) : p(value) {}
        Observer& operator++() { p++; return *this; }
        Observer operator++(int) { int *temp = p; p++; return Observer(temp); }
        bool operator==(Observer other) const { return p == other.p; }
        bool operator!=(Observer other) const { return p != other.p; }
        int& operator*() const { return *p; }
    };

    Observer begin() { return Observer(arr); }
    Observer end() { return Observer(arr + n - 1); }
};
Run Code Online (Sandbox Code Playgroud)

Source.cpp

int main()
{
    SimpleArray array(5);
    cin >> array;

    for (int item : array)
    {
        cout << item << " ";
    }

    cin.ignore();
    cin.get();
}
Run Code Online (Sandbox Code Playgroud)

输入

1 2 3 4 5
Run Code Online (Sandbox Code Playgroud)

begin()end()函数是公共的,并Observer封装了循环运行所需的所有操作符.编译的代码.

产量

1 2 3 4 5
Run Code Online (Sandbox Code Playgroud)

在尝试相同之后 std::for_each

std::for_each(array.begin(), array.end(), [](int item) { cout << item << " "; });
Run Code Online (Sandbox Code Playgroud)

我有一些编译器错误:

C2062: type 'unknown-type' unexpected   Observer
C2938: '_Iter_cat_t<SimpleArray::Observer>' : Failed to specialize alias template
C2794: 'iterator_category': is not a member of any direct or indirect base class of 'std::iterator_traits<_InIt>'
Run Code Online (Sandbox Code Playgroud)

在阅读了关于for_each之后,我发现它的类型参数必须满足一些要求,简而言之 - 就是迭代器.

我现在的问题是:如果创建一个提供迭代功能的常规类相当容易,为什么这个函数(以及其他许多其他函数)以强制执行此标准的方式设计?

Jor*_*elo 6

唯一std::iterator提供是成员类型iterator_category,value_type,difference_type,pointer,和reference,这是从您提供的模板参数定义.它没有任何其他成员.您可以自己定义这些成员类型.

真正的答案是你根本不需要继承std::iterator它并没有给你任何真正的优势.就C++而言,如果它看起来像一个迭代器,它就是一个迭代器.您只需要确保您的迭代器类型遵循此处概述的概念:http://en.cppreference.com/w/cpp/concept/Iterator

std::iterator 在C++ 17中被弃用,因为它相对无用.

  • _"如果它看起来像一个迭代器,它是一个迭代器"_或者也称为[duck typing](https://en.wikipedia.org/wiki/Duck_typing) (2认同)