我正在爲我的科學軟件編寫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