简体   繁体   English

重载++运算符以增加迭代器

[英]Overloading the ++ operator to increment an iterator

I am currently creating a square list and one of the requirements is to overload the the pre- and postfix ++ operator. 我当前正在创建一个方形列表,要求之一是重载prefix和postfix ++运算符。

I've tried to overload the ++operator in my .hpp file to increment an iterator. 我试图在我的.hpp文件中重载++ operator以增加迭代器。 But when calling the ++operator it does not call the overloaded code and just uses the default. 但是,当调用++ operator时,它不会调用重载代码,而仅使用默认值。

iterator& operator ++ () {  // pre-increment 
        std::list<int>::iterator i = list_Values.begin(); 
        advance(i,1); 
        return &*i; 
    } 

    iterator operator ++ (int) { // post-increment 
        std::list<int>::iterator i = list_Values.begin(); 
        advance(i,1); 
        return &*i; 
    } 

I try to invoke the ++operator this way: 我尝试通过这种方式调用++ operator:

int* test = sqrList.begin();
  ++test;

  test++;

You have to declare the operator inside the class. 您必须在类中声明运算符。

class Iterator
{
public:
    Iterator& operator ++ ()
    {
       return *this;
    }
    Iterator operator ++ (int)
    {
       return *this;
    }
};

Also, why are you using return &*i; 另外,为什么要使用return &*i; ?

Note that these operators need to be declared inside the Iterator class, not inside the container. 请注意,这些运算符需要在Iterator类中声明,而不是在容器中声明。

int* test = sqrList.begin();
++test;
test++;

You're incrementing a int* , and since there's no int* class to extend, you cannot possibly have actually created operators for this type. 您将要递增一个int* ,并且由于没有要扩展的int*类,因此您实际上可能无法为此类型创建运算符。

Create the operators for the correct type, instead. 而是为正确的类型创建运算符。 You probably mean to make your own Iterator type. 您可能打算制作自己的Iterator类型。

For novices it is sometimes hard to understand an issue out of context. 对于新手来说,有时很难从上下文中理解问题。 So I decided to provide a more or less complete example of defining custom iterator and overloading its operators. 因此,我决定提供一个或多或少完整的示例来定义自定义迭代器并重载其运算符。 Notice, when overloading post increment operator, we return iterator by value, not by reference. 注意,当重载后置增量运算符时,我们按值而不是引用返回迭代器。 Of course, this is not a real world example, where we would not use raw pointers. 当然,这不是真实的示例,在该示例中,我们将不使用原始指针。

#include<iostream>
#include<assert.h>

struct node
{
    int data;
    node* next;
    node() :data(0), next(nullptr) {}
    node(int d) :data(d), next(nullptr) {}
};

//forward declaration
class MyList;

class MyListIterator
{
private:
    node* _ptr;
    friend class MyList;
    explicit MyListIterator(node* n) : _ptr(n) {}
public:
    int& operator*() const {
        return _ptr->data;
    }
    //overload pre increment operator
    MyListIterator& operator++() {
        _ptr = _ptr->next;
        return *this;
    }
    // overload post increment operator
    MyListIterator operator++(int) {
        MyListIterator ret = *this;
        ++* (this);
        return ret;
    }

    bool operator==(const MyListIterator& iter) const {
        return this->_ptr == iter._ptr;
    }

    bool operator!=(const MyListIterator& iter) const {
        return this->_ptr != iter._ptr;
    }
};

class MyList
{
private:
    node* _head;
    node* _tail;
    size_t _size;
public:
    MyList() : _head(nullptr), _tail(nullptr), _size(0) {};

    void push_back(int d) {
        node* newTail = new node(d);
        if (_tail != nullptr) {
            _tail->next = newTail;
        }
        else {
            _head = newTail;
        }
        _tail = newTail;
        _size++;
    }

    int& operator[](size_t i) {
        if (i >= _size)
            throw std::out_of_range("MyList");
        node * p = _head;
        for (size_t j = 0; j < i; j++) {
            p = p->next;
        }
        return p->data;
    }

void remove(int d) {
    if (_head == nullptr)
        return;
    node * p = _head;
    node * prev = p;
    while ((p != nullptr) && (p->data != d)) {
        prev = p;
        p = p->next;
    }
    if (p != nullptr) {
        if (p == _head)
            _head = p->next;
        else
            prev->next = p->next;
        delete p;
        _size--;
    }
}

    size_t size() const {
        return _size;
    }

    ~MyList() {
        node* next = nullptr;
        for (node* p = _head; p != nullptr; p = next) {
            next = p->next;
            delete p;
        }
    }
    using iterator = MyListIterator;
    iterator begin() { return iterator(_head); }
    iterator end() { return iterator(_tail->next); }
};

int main()
{

    MyList mylist;
    mylist.push_back(1);
    mylist.push_back(2);
    mylist.push_back(3);
    int count = 1;
    //pre increment
    for (MyListIterator it = mylist.begin(); it != mylist.end(); ++it)
        assert(*it == count++);
    count = 1;
    //post increment
    for (MyListIterator it = mylist.begin(); it != mylist.end(); it++)
        assert(*it == count++);
    for (size_t i = 0; i < 3; i++)
        assert(mylist[i] == i + 1);

    mylist.remove(2);
    assert(mylist[0] == 1);
    assert(mylist[1] == 3);
    assert(mylist.size() ==2);

    mylist.remove(1);
    mylist.remove(3);
    assert(mylist.size() == 0);

}

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

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