我正在为我的科学软件编写3D网格,我需要遍历网格的节点以获取它们的坐标。我不想将容器中的每个节点对象都放在容器中,而是想在迭代时快速计算坐标。问题是stl :: iterator需要返回对operator*()
或operator->()
的结果的值的引用。STL迭代器:“取消引用”迭代器到临时。可能吗?
下面的一些代码的:
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);
}
此方法查询节点坐标由索引提供
spVector3D spGrid::GetPoint(int idx) const {
// spVector3D vec = ... calculate the coordinates here ...
return vec;
}
在此的任何输入?
由于提前, 伊利亚
“问题是stl :: iterator需要返回对运算符*()的结果的引用,或者运算符 - >()的指针”为什么会有这个问题?为了什么? – 2010-10-29 09:07:52
我不确定我真的明白你想要达到什么目的。也许你可以包含一个客户端代码的例子,以了解你如何使用这个集合。 – 2010-10-29 09:13:39
“为什么这是一个问题?为什么?”因为这样我就不得不保留一个实际的变量来指向(或引用)。 – ezpresso 2010-10-29 11:11:51