简体   繁体   English

使用Node *作为列表的迭代器

[英]Using `Node*` as iterator for a list

#include <iostream>
#include <algorithm>

struct Node
{
    int value_;
    Node* next_;

    Node(int value, Node* next = nullptr)
        : value_(value)
        , next_(next)
    {}
};

Node* operator++(Node* node)
{
    node = node->next_;
    return node;
}

int operator*(Node* node)
{
    return node->value_;
}

int main()
{
    Node* first = new Node(10);
    first->next_ = new Node(20);
    first->next_->next_ = new Node(17);

    Node* endIter = nullptr;

    std::cout << std::accumulate(first, endIter, 0) << std::endl;
}

In this example I have tried to use Node* as iterator for list. 在此示例中,我尝试使用Node*作为列表的迭代器。 I am getting compiler errors 我收到编译器错误

  1 main.cpp:15:28: error: Node* operator++(Node*) must have an argument of class or enumerated type
  2  Node* operator++(Node* node)
  3                             ^
  4 main.cpp:21:25: error: int operator*(Node*) must have an argument of class or enumerated type
  5  int operator*(Node* node)

Looks like I can't overload operator++ and operator* for pointers. 看起来我无法重载operator++operator*的指针。

I have copied this overloads from the book Stroustrup: The C++ Programming Language (4th Edition) pg 703 . 我已经从《 Stroustrup: The C++ Programming Language (4th Edition) pg 703复制了此重载。

Can anyone explain what I have done wrong? 谁能解释我做错了什么?

The input to std::accumulate must meet the requirements of an InputIterator . std::accumulate的输入必须满足InputIterator的要求。

One of the requirements of an InputIterator is that it support the pre-increment operator. InputIterator的要求之一是它支持预增量运算符。

You can use the pre-increment operator on a Node* but it will use the built-in logic to increment the pointer. 您可以在Node*上使用pre-increment运算符,但是它将使用内置逻辑来增加指针。

Node* operator++(Node* node) { ... }

is invalid since type of the argument is Node* . 无效,因为参数的类型为Node* You can overload operator++ for Node but not Node* . 您可以为Node重载operator++ ,但不能重载Node*

From the C++11 Standard (emphasis mine): 从C ++ 11标准(重点是我的):

13.5 Overloaded operators 13.5重载的运算符

6 An operator function shall either be a non-static member function or be a non-member function and have at least one parameter whose type is a class, a reference to a class, an enumeration, or a reference to an enumeration . 6操作员函数应为非静态成员函数或为非成员函数,并具有至少一个参数,其类型为类,对类的引用,枚举或对枚举的引用

You can't overload operators for primitive type or point. 您不能重载原始类型或点的运算符。 So you should write a iterator for Node . 因此,您应该为Node编写一个迭代器。

class iterator {
public:
  iterator(Node *node): _node(node) {}
  iterator operator++() {
    _node = _node->next;
    return *this;
  }
  iterator operator++(int) {
    iterator tmp = *this;
    ++(*this);
    return tmp;
  }
  bool operator == (const iterator &iter) const {
    return _node == iter._node;
  }
  int operator*() {
    return _node->value;
  }
private:
  Node *_node;
};

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM