1

I am trying to create a square_list and I am doing so by creating a vector<list<T>>. Each column in the square list is a separate list in the vector. When I create a list<T>::iterator, is there a way to overload the ++ operator? I need to overload it so that I can jump from the end of one column list to the start of the next column list. Reworking the whole thing to create the square list is not a very viable option at this point. Thanks!

Cœur
  • 37,241
  • 25
  • 195
  • 267
  • Specifically look at [Unary arithmetic operators](http://stackoverflow.com/a/4421719/2296458) – Cory Kramer Aug 13 '14 at 23:49
  • 5
    @Cyber: though technically correct, this is an XY problem and you're leading down the wrong path. He needs to make a custom iterator, not alter the existing `std::list::iterator` to jump columns. – Mooing Duck Aug 13 '14 at 23:50
  • Do you think you could give me a quick tutorial on how to do that? The only thing I need to change is the ++ implementation. It would be much appreciated! –  Aug 13 '14 at 23:56
  • So you have a `vector` of `list`s, and you want to iterate through all the elements inside all the lists basically? – notadam Aug 14 '14 at 00:42
  • Yeah, that's exactly it. –  Aug 14 '14 at 00:51

1 Answers1

1

For demonstrating the idea only, here is an incomplete design and test program:

class square_list_iterator
    :
    public list<int>::iterator
{
    vector<list<int>>::iterator outer_it;

public:
    square_list_iterator(vector<list<int>>& sl)
    :
        list<int>::iterator{sl.begin()->begin()},
        outer_it{sl.begin()}
    {}
    square_list_iterator& operator++()
    {
        list<int>::iterator::operator++();
        if (*this == outer_it->end())
        {
            ++outer_it;
            list<int>::iterator::operator=((*outer_it).begin());
        }
        return *this;
    }
};

int main()
{
    vector<list<int>> sl{{1,2,3},{4,5,6}};
    square_list_iterator it{sl};

    cout << *it << endl;
    ++it;
    cout << *it << endl;
    ++it;
    cout << *it << endl;
    ++it;
    cout << *it << endl;
    ++it;
    cout << *it << endl;
    ++it;
    cout << *it << endl;
    ++it;
    return 0;
}

Once again, it is incomplete. It is just an idea.

The idea is to create a custom iterator by inheriting list::iterator and hide the original operator++ by creating your own.

You may also need to implement other operators and constructors and member functions to complete it. A pair of non-member begin() and end() may help. This iterator can be a member type of your square_list class template.


To elaborate:

  1. I am not doing what you ask. I am not overloading the increment operator. I am hiding it and creating a new one to replace it.
  2. I agree with Mooing Duck's comment. We should not alter std::list<T>::iterator. We should create a custom iterator instead. Be aware of Open-Close Principle.
  3. You say that you don't want to create an iterator from scratch. So, I suggest inheritance. But please be aware of the drawbacks and alternatives.
  4. Overloading does not work between base class and derived class.