Overloaded operator `! = `It does not work correctly in a custom iterator class

I am creating a template matrix class, and now I am implementing an iterator class to iterate over a single column (this iterator class is inside my class Matrix<T>).

template <typename P, typename V> // "P" - PointerType; "V" - ValueType
class V_Iterator : public std::iterator<std::forward_iterator_tag, T> {
private:
    P itData_;
public:
    size_type w; // width of the matrix
    size_type h; // height of the matrix
public:
    V_Iterator(P d) : itData_(d) { }

public:
    V& operator*() const {
        return *itData_;
    }
    /////////////////// BUG /////////
    V_Iterator<P, V>& operator++() {

        itData_ += w;

        return *this;
    }
    V_Iterator<P, V>& operator= (T value) {
        *itData_ = value;
        return *this;
    }
    P operator->() {
        return itData_;
    }
    friend bool operator==(const V_Iterator& lhs, const V_Iterator& rhs) {
        return !(lhs.itData_ != rhs.itData_);
    }
    friend bool operator!=(const V_Iterator& lhs, const V_Iterator& rhs) {
        return !(lhs.itData_ == rhs.itData_);
    }

    V_Iterator<P, V> begin(size_type column) { return V_Iterator<P, V>(itData_ + column); }
    V_Iterator<P, V> end(size_type column) { return V_Iterator<P, V>((itData_ + column) + 1 + (h - 1) * w ); };
};

My problem is when I repeat using my iterator:

Matrix<int> m (5, 5);
for (Matrix<int>::viterator vit = m.v_begin(0); vit != m.v_end(0); ++vit) {
    cout << *vit << " ";
}

it never stops because it !=never starts. However, if the ++operator is overloaded , it is a code instead of the one that I already posted

V_Iterator<P, V>& operator++() {
    ++itData_;
    return *this;
}

It works great.

Here is the code inside the Matrix class:

public:
V_Iterator<T*, T> m_viterator_;
V_Iterator<const T*, const T> m_const_viterator_;

typedef V_Iterator<T*, T> viterator;
typedef V_Iterator<const T*, const T> const_viterator;

viterator v_begin(size_type column) { return m_viterator_.begin(column); }
viterator v_end(size_type column) { return m_viterator_.end(column); }
const_viterator cv_begin(size_type column) { return m_const_hiterator_.begin(column); }
const_viterator cv_end(size_type column) { return m_const_hiterator_.end(column); }

Matrix class constructor:

Matrix(const size_type& width, const size_type& height) :   width_(width),
                                                            height_(height),
                                                            data_(CreateMatrix(width, height)),                                                                m_viterator_(*data_),
                                                            m_const_viterator_(*data_) {

    m_viterator_.w = width_;
    m_viterator_.h = height_;
    m_const_viterator_.w = width_;
    m_const_viterator_.h = height_;

    // fill the created matrix with default values of "T"
    for (Matrix<T>::iterator_type it = this->begin(); it != this->end(); ++it)
        *it = T();
}

I tried to find out what was wrong, but for me everything looks fine.

+4
source share
2 answers

, - begin() end(), w h. m_viterator, - begin() end() , itData_, w h .

+1

, , w . - . , , , w. , begin() end().

V_Iterator<P, V> begin(size_type column)
{
     return V_Iterator<P, V>(itData_ + column);
}

V_Iterator<P, V> end(size_type column)
{
     return V_Iterator<P, V>((itData_ + column) + 1 + (h - 1) * w);
}

, , begin (n) end end (n) end (n) -begin (n) :

(itData_ + column) + 1 + (h - 1) * w - (itData_ + column)

1 + (h - 1) * w

: ++ w , , begin end, .

, , w!

+1

Source: https://habr.com/ru/post/1622341/


All Articles