2015-10-26 135 views
0

我在C++標準庫中使用了一個列表,我想知道如何使用迭代器獲得第二個元素。我有兩個迭代器,我希望迭代器2前進1個節點。如何獲得標準列表中的第二個元素

intPaint(string input){ 
list<char>sequence (input.begin,input.end); 
int count; 
list<char>::iterator ptr1= sequence.begin(); 
list<char>::iterator ptr2= ?// I want to get the second node 
...//rest of the code isn't important it just checks the characters and moves 
//the iterator 
} 
+0

[皮克在STL容器的下一個元素]的可能的複製(http://stackoverflow.com/questions/3673684/peek-the-next-element-in-stl-container) –

回答

1

如果你確實使用C++ 11,然後std::next()應該做的伎倆:

list<char>::iterator ptr1 = sequence.begin(); 
list<char>::iterator ptr2 = std::next(sequence.begin()) 

documentation

template <class ForwardIterator> 
ForwardIterator next(ForwardIterator it, 
        typename iterator_traits<ForwardIterator>::difference_type n = 1); 

獲取迭代器下一個元素。返回一個指向 元素的迭代器,它將指向高級n位置的元素。

+0

感謝儘管這沒」 t做我想要的工作,但無論如何感謝你 – btm

+0

這很好,但如果答案錯了,我可以刪除或修復它:-)你的意思是它正確回答了這個問題,但你的問題不是你的方式實際問題? – WhiteViking

+0

不,我試過,但對於我試圖使用列表的結果是不起作用的問題,所以我需要使用不同的數據結構。 – btm

相關問題