2012-03-29 53 views
2

我有一個自定義迭代器模板類,它包裝了一個std::list迭代器。在我的hasNext()方法中,我想檢查迭代器是否引用列表中的最後一項。然而這段代碼:無法將std :: list迭代器與list :: back()進行比較?

template< class Type > 
... 
virtual bool hasNext(void) 
{ 
    return itTypes != pList->back(); 
} 
... 
typename std::list<Type> * pList; 
typename std::list<Type>::iterator itTypes; 

產生錯誤:

error C2678: binary '!=' : no operator found which takes a left-hand operand of type 'std::_List_iterator<_Mylist>'

如果我比較itTypeslist::begin()list::end()我沒有得到任何問題。我在STL迭代器中查看的文檔說std :: list使用的雙向迭代器支持相等和不等式比較運算符。有沒有一個原因,我不能比較list::back()呢?

回答

7

你想要end()back()獲取最後一個元素的,它根本不是迭代器。

+1

爲什麼最討厭的蟲子總是最簡單的:/感謝您的幫助:) – PeddleSpam 2012-03-29 01:26:25

1

back()返回的元素不是迭代器。 所以使用代碼

return *itTypes != pList->back() 
1

begin()end()函數返回迭代器而front()back()返回元素引用

相關問題