简体   繁体   English

查看 STL 容器中的下一个元素

[英]Peeking the next element in STL container

Is it possible to peek next element in a container which the iterator currently points to without changing the iterator?是否可以在不更改迭代器的情况下查看迭代器当前指向的容器中的下一个元素?

For example in std::set,例如在 std::set 中,

int myArray[]= {1,2,3,4};
set <int> mySet(myArray, myArray+4);
set <int>::iterator iter = mySet.begin();

//peek the next element in set without changing iterator.

mySet.erase(iter); //erase the element if next element is n+1

C++0x adds a handy utility function, std::next , that copies an iterator, advances it, and returns the advanced iterator. C++0x 添加了一个方便的实用程序函数std::next ,它复制迭代器、推进它并返回高级迭代器。 You can easily write your own std::next implementation:您可以轻松编写自己的std::next实现:

#include <iterator>

template <typename ForwardIt>
ForwardIt next(ForwardIt it, 
               typename std::iterator_traits<ForwardIt>::difference_type n = 1)
{
    std::advance(it, n);
    return it;
}

You can use this in your example like so:您可以在示例中使用它,如下所示:

if (iter != mySet.end() && next(iter) != mySet.end() && *next(iter) == *iter + 1)
    mySet.erase(iter);

Not with iterators in general.一般不使用迭代器。 An iterator isn't guaranteed to be able to operate non-destructively.不能保证迭代器能够非破坏性地运行。 The classic example is an Input Iterator that actually represents an underlying input stream.经典示例是实际表示底层输入流的输入迭代器。

There's something that works for this kind of iterator, though.不过,有些东西适用于这种迭代器。 A Forward Iterator doesn't invalidate previous copies of itself by the act of moving forward through the collection.前向迭代器不会通过在集合中向前移动而使自身的先前副本无效。 Most iterators (including those for STL collections) are at least Forward Iterators, if not a more functional version- only Input Iterators or Output Iterators are more restricted.大多数迭代器(包括用于 STL 集合的迭代器)至少是前向迭代器,如果不是功能更强大的版本 - 只有输入迭代器或输出迭代器受到更多限制。 So you can simply make a copy of your iterator, increment the copy and check that , then go back to your original iterator.所以你可以简单地复制你的迭代器,增加副本并检查,然后回到你的原始迭代器。

So your peek code:所以你的偷看代码:

set <int>::iterator dupe = iter;
++dupe;
// (do stuff with dupe)
set <int>::iterator iter2 = iter;
++iter2;
int peekedValue = *iter2;

You can always make a copy of the iterator and advance the copy:您始终可以制作迭代器的副本并推进副本:

set <int>::iterator iter = mySet.begin();
set <int>::iterator iterCopy = iter;
iterCopy++;
if (*iterCopy == something)
  mySet.erase(iter);

But beware that iterCopy may no longer be valid once you erase iter .但请注意,擦除iteriterCopy可能不再有效。

对于序列容器(向量、双端队列和列表),您可以调用 front 来查看(有关此链接下部的更多信息)。

This will not work for std::set as its nature does not allow for the [] operator, but for containers that do, you can do:这对std::set不起作用,因为它的性质不允许 [] 运算符,但对于这样做的容器,您可以执行以下操作:

std::vector<int> v;
v.push_back(3);
v.push_back(4);
std::vector<int>::iterator it = v.begin(); 
std::cout << v[it - v.begin() + 1];

But this could be dangerous if it points to the last element in the container;但是如果it指向容器中的最后一个元素,这可能是危险的; but the same applies to the solution above.但同样适用于上述解决方案。 Eg you'll have to make checks in both cases.例如,您必须在两种情况下进行检查。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM