STL迭代器:“解除引用”迭代器到临时的。可能吗? [英] STL iterator: "dereferencing" iterator to a temporary. Is it possible?

查看:101
本文介绍了STL迭代器:“解除引用”迭代器到临时的。可能吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在为我的科学软件编写一个3D网格,我需要遍历网格的节点来获取它们的坐标。我没有把每个节点对象都放在容器中,而是想在迭代时动态计算坐标。问题是stl :: iterator需要返回对 operator *()的结果的引用,或 operator - >的指针( )

I'm writing a 3D grid for my scientific software and I need to iterate through the nodes of the grid to get their coordinates. Instead of holding each node object in the container I'd rather like to just calculate the coordinates on the fly while iterating. The problem is that stl::iterator requires to return reference to a value as a result of operator*(), or pointer for operator->().

以下部分代码:


class spGridIterator {
public:
    typedef forward_iterator_tag iterator_category;
    typedef spVector3D value_type;
    typedef int difference_type;
    typedef spVector3D* pointer;
    typedef spVector3D& reference;

    spGridIterator(spGrid* gr, int index);

    spGridIterator& operator++();
    spGridIterator& operator++(int);

    reference operator*() const;
    pointer operator->() const;

private:
    spGrid* m_grid;
    int m_idx;
};




spGridIterator::reference spGridIterator::operator*() const {
    // return m_grid->GetPoint(m_idx);
}

spGridIterator::pointer spGridIterator::operator->() const {
    // return m_grid->GetPoint(m_idx);
}

此方法按提供的索引查询节点坐标

This method queries the node coordinates by index provided


spVector3D spGrid::GetPoint(int idx) const {
    // spVector3D vec = ... calculate the coordinates here ...
    return vec;
}

有关此的任何输入吗?

提前致谢,
Ilya

Thanks in advance, Ilya

推荐答案

您可以使用成员变量来保存网格点目前指向:

You could use a member variable to hold the grid point it is currently pointing to:

class spGridIterator {
public:
    typedef forward_iterator_tag iterator_category;
    typedef spVector3D value_type;
    typedef int difference_type;
    typedef spVector3D* pointer;
    typedef const spVector3D* const_pointer;
    typedef const spVector3D& const_reference;
    typedef spVector3D& reference;

    spGridIterator(spGrid* gr, int index);

    spGridIterator& operator++();
    spGridIterator& operator++(int);

    reference operator*();
    const_reference operator*() const;

    pointer operator->();
    const_pointer operator->() const;

private:
    spGrid* m_grid;
    int m_idx;
    mutable spVector3D CurrentPoint;
};

然后取消引用运算符可能如下所示:

Then the dereference operator could look like this:

spGridIterator::const_reference spGridIterator::operator*() const {
    CurrentPoint = m_grid->GetPoint(m_idx);
    return CurrentPoint;
}

感谢@greg指出 CurrentPoint 需要 mutable 才能生效。这将是一个惰性实现(仅在迭代器实际解除引用时检索点)。急切的实现将更新迭代器的mutator方法中的 CurrentPoint 成员(在此示例中为 operator ++ 变体),使可变多余。

Thanks to @greg for pointing out that CurrentPoint needs to be mutable for this to work. This would be a lazy implementation (only retrieving the point when the iterator is actually dereferenced). An eager implementation would update the CurrentPoint member in the mutator methods of the iterator (the operator++ variants in this example), making the mutable superfluous.

这篇关于STL迭代器:“解除引用”迭代器到临时的。可能吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆