如何找出迭代器是否指向最后一个(结束)节点? [英] How to find out a if an iterator is pointing to the very last (end) node?
问题描述
我该怎么做?
问题是,关于迭代器的唯一信息是
迭代器本身。没有它所属的容器或任何东西。喜欢
模板< Iteratorvoid totally_isolated(Iterator& it)
{
//我怎么知道它是否指向结束节点?
}
像std :: vector :: iterator,std :: list :: iterator等迭代器是
通常传递给totally_isolated()函数。用户定义
迭代器也可能确实传递给函数。
有没有安全便携的解决方案?这是我的目标:我希望通过* -operator访问
访问引用的值。我不能这样做,如果
迭代器指向最后一个节点(如果我这样做,则会抛出一个异常
或断言会失败) 。
而不是检索引用的值,我希望函数做什么?b $ b'特别''(比如在屏幕上打印一条消息)
迭代器指向结束节点。
有诀窍吗?
How do I do that?
The thing is, the only information I have about the iterator is the
iterator itself. No container it is belonging to or anything. Like
template<Iteratorvoid totally_isolated(Iterator& it)
{
//how do I find out if it points to the end node?
}
Iterators like std::vector::iterator, std::list::iterator and so on are
usually passed to the totally_isolated() function. User defined
iterators may indeed be passed to the function as well.
Is there any safe and portable solution? This is my goal: I want to
access the referenced value via the *-operator. I can''t do this if if
the iterator is pointing to the very last node (if I did, an exception
would be thrown or an assertion would fail).
Instead of retrieving the referenced value, I want the function to do
something ''special'' (like printing a message to the screen) whenever
the iterator is pointing to the end node.
Is there any trick?
推荐答案
ma*******@googlemail.com 写道:
单手拍手的声音是什么?
一对迭代器指定一系列值。一个单独的迭代器,没有上下文的
告诉你什么。
-
- Pete
标准C ++库扩展:一个教程和
参考的作者。有关本书的更多信息,请参阅
www.petebecker.com/tr1book。
ma*******@googlemail.com wrote:
What is the sound of one hand clapping?
A pair of iterators designates a sequence of values. A single iterator,
without context, tells you nothing.
--
-- Pete
Author of "The Standard C++ Library Extensions: a Tutorial and
Reference." For more information about this book, see
www.petebecker.com/tr1book.
一对迭代器指定一系列值。单个迭代器,
A pair of iterators designates a sequence of values. A single iterator,
没有上下文,什么都不告诉你。
without context, tells you nothing.
Pete,即使我有一对迭代器,也没用。将是
仍然是同样的问题。
Pete, even if I had a pair of iterators, it would be no use. Would be
still the same problem.
ma ******* @ googlemail.com 写道:
> A迭代器对指定一系列值。没有上下文的单个迭代器不会告诉你什么。
>A pair of iterators designates a sequence of values. A single iterator,
without context, tells you nothing.
Pete,即使我有一对迭代器,也没用。将是
仍然是同样的问题。
Pete, even if I had a pair of iterators, it would be no use. Would be
still the same problem.
如果对中的两个迭代器相等,则序列为空,并且
第一个迭代器没有指向有效元素。如果两者不等于
,则第一个迭代器指向有效元素。
听起来实际问题是代码在使用迭代器
非惯用方式。不要那样做。
-
- Pete
标准C ++库扩展:教程和
参考。有关本书的更多信息,请参阅
www.petebecker.com/tr1book。
这篇关于如何找出迭代器是否指向最后一个(结束)节点?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!