检查迭代器是否属于列表 [英] Check whether iterator belongs to a list
问题描述
有什么方法可以检查给定的迭代器是否属于C ++中的给定列表。
Is there any way to check whether a given iterator belongs to a given list in C++?
推荐答案
方法
您不能简单地遍历列表,将每个迭代器值与您的候选值进行比较。
The obvious but invalid approach
You can't simply iterate through the list, comparing each iterator value to your "candidate".
C ++ 03标准对于应用于不同容器的迭代器的 ==
的有效性(Mankarse对Nawaz的回答链接 http://www.open-std.org/jtc1/sc22/wg21/ docs / papers / 2009 / n2948.html#446 ),一些编译器(例如VC ++ 2005调试模式)警告,如果这样做,但尽管它可能实际工作可靠取决于您的编译器/库
The C++03 Standard is vague about the validity of ==
applied to iterators from different containers (Mankarse's comment on Nawaz's answer links http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2009/n2948.html#446), some compilers (eg. VC++2005 debug mode) warn if you do so, but despite all that it may actually work reliably depending on your compiler/libraries - check its documentation if you don't care about portability.
C ++ 11标准非常明确,不能将迭代器与不同容器进行比较:
The C++11 Standard is very explicit, you can't compare iterators to different containers:
§24.2.5正向迭代器的域==是同一底层序列上的迭代器。 / p>
§ 24.2.5 The domain of == for forward iterators is that of iterators over the same underlying sequence.
因此,依赖于 operator ==
的问题的答案是有问题的
So, the answers to this question that rely on operator==
are questionable now, and invalid in future.
你可以
-
Mankarse的评论警告说,这可能不适用于提供自己的
运算符&
的对象。您可以使用std :: addressof
或C ++ 03 boost的版本
Mankarse's comment cautions that this might not work as intended for objects providing their own
operator&
. You could work around this usingstd::addressof
, or for C++03 boost's version
Martin的注释提到,您必须假定候选迭代器,您正在测试列表成员资格是安全地解引用 - 到它来自的容器的 end()
迭代器。
Martin's comment mentions that you have to assume the candidate iterator that you're testing list membership for is safely dereferenceable - i.e. not equal to an end()
iterator on the container from which it came. As Steve points out - that's a pretty reasonable precondition and shouldn't surprise anyone.
(这对所有人来说都是可以的。)这是一个非常合理的前提条件,作为存储元素的标准容器从不具有相同的地址,但更一般地,用户定义的容器可以允许不相等的迭代器寻址相同的值对象(例如支持循环或飞重模式样式优化),在这种情况下,但是,如果你写这样的容器,你可能会为安全的迭代器比较而设计。)
(This is fine for all Standard containers as stored elements never have the same address, but more generally user-defined containers could allow non-equal iterators to address the same value object (e.g. supporting cycles or a "flyweight pattern" style optimisation), in which case this approach would fail. Still, if you write such a container you're probably in a position to design for safe iterator comparison.)
实现:
template <class IteratorA, class IteratorB, class IteratorC>
inline bool range_contains(IteratorA from, const IteratorB& end,
const IteratorC& candidate)
{
while (from != end)
if (&*from++ == &*candidate)
return true;
return false;
}
注意:
- 这采用接受一系列迭代器位置进行搜索的标准库方法。
- 每个迭代器的类型允许变化,因为有可移植性问题,例如container
begin()
返回迭代器
,但end()
返回const_iterator
。 -
以外的迭代器取自
const
引用,因为迭代器有时可能是非平凡的对象(即太大,不适合寄存器,复制相对昂贵)。 $>是需要的值,因为它会在范围内递增。
- This adopts the Standard library approach of accepting a range of iterator positions to search.
- The types of each iterator are allowed to vary, as there are portability issues, e.g. containers where
begin()
returns aniterator
butend()
returns aconst_iterator
. - Iterators other than
from
are taken byconst
reference, as iterators can sometimes be non-trivial objects (i.e. too large to fit in a register, relatively expensive to copy).from
is needed by value as it will be incremented through the range.
这篇关于检查迭代器是否属于列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!