C++ 查看 STL 容器中的下一个元素
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3673684/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Peeking the next element in STL container
提问by user963241
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
采纳答案by Adam Norberg
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 Iteratordoesn'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. So you can simply make a copy of your iterator, increment the copy and check that, then go back to your original iterator.
不过,有些东西适用于这种迭代器。一个前向迭代器不通过收集向前移动的行为无效的自身以前的副本。大多数迭代器(包括用于 STL 集合的迭代器)至少是前向迭代器,如果不是功能更强大的版本 - 只有输入迭代器或输出迭代器受到更多限制。所以你可以简单地复制你的迭代器,增加副本并检查它,然后回到你的原始迭代器。
So your peek code:
所以你的窥视代码:
set <int>::iterator dupe = iter;
++dupe;
// (do stuff with dupe)
回答by James McNellis
C++0x adds a handy utility function, std::next
, that copies an iterator, advances it, and returns the advanced iterator. You can easily write your own std::next
implementation:
C++0x 添加了一个方便的实用函数,std::next
,它复制一个迭代器,推进它,并返回高级迭代器。您可以轻松编写自己的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);
回答by Andrew Shepherd
set <int>::iterator iter2 = iter;
++iter2;
int peekedValue = *iter2;
回答by casablanca
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
.
但请注意,iterCopy
擦除后可能不再有效iter
。
回答by skimobear
回答by identity
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; but the same applies to the solution above. E.g. you'll have to make checks in both cases.
但是如果it
指向容器中的最后一个元素,这可能是危险的;但同样适用于上述解决方案。例如,您必须在两种情况下进行检查。