我正在使用 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
}
最佳答案
如果您确实使用 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);
Get iterator to next element. Returns an iterator pointing to the element that it would be pointing to if advanced n positions.
关于c++ - 如何获取标准列表中的第二个元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33355356/