[英]Overloaded `!=` operator in custom iterator class is not working properly
I am creating template matrix class and right now I am implementing iterator class to iterate through one column (this iterator class is inside my Matrix<T>
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 that when I am iterating 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 !=
is never triggered. 它永远不会停止,因为
!=
永远不会被触发。 However if in overloading ++
operator is this code instead of one I already posted 但是,如果在重载
++
运算符是这个代码而不是我已发布的代码
V_Iterator<P, V>& operator++() {
++itData_;
return *this;
}
It is working completely fine. 它工作得很好。
Here is more code inside my Matrix class: 这是我的Matrix类中的更多代码:
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
类构造函数:
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 was trying to figure out what is wrong but to me everything seems ok. 我试图弄清楚出了什么问题,但对我来说一切似乎都没问题。
As it's being pointed out in the comments, it's not shown how the w
member gets initialized. 正如评论中指出的那样,没有显示
w
成员如何初始化。 However, I also spotted something else. 但是,我也发现了别的东西。 I'm going to assume that the rest of the code that's not shown is going to initialize
w
correctly. 我将假设未显示的其余代码将正确初始化
w
。 Let's look at how you defined begin() and end(). 让我们看一下你如何定义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);
}
So, it looks to me like the distance between begin(n) end end(n) or end(n)-begin(n) is: 所以,在我看来,begin(n)end end(n)或end(n)-begin(n)之间的距离是:
(itData_ + column) + 1 + (h - 1) * w - (itData_ + column)
or 要么
1 + (h - 1) * w
You have stated the problem as follows: when your ++ operator keeps advancing the iterator by w
each time, the loop iterating from begin
to end
will never compare equal to the ending iterator value. 你已经说明了这个问题如下:当你的++运算符每次都按
w
推进迭代器时,从begin
到end
迭代的循环永远不会比较等于结束迭代器值。
Of course not, because the distance between them will never be an even multiple of w
! 当然不是,因为它们之间的距离永远不会是
w
的偶数倍!
The iterator returned by the begin()
and end()
member functions does not have w
and h
initialised. 由
begin()
和end()
成员函数返回的迭代器没有w
和h
初始化。 You set them in the m_viterator
member, but the member functions begin()
and end()
of the iterator class return an iterator initialised from the itData_
member only, which leaves w
and h
in the newly created iterator uninitialised. 您可以在
m_viterator
成员中设置它们,但迭代器类的成员函数begin()
和end()
返回itData_
成员初始化的迭代器,这将在新创建的迭代器中itData_
未初始化的w
和h
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.