如何使迭代器无效? [英] How to invalidate an iterator?

查看:30
本文介绍了如何使迭代器无效?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

由于我知道程序中的迭代器可能会因某些先前的操作而无效,因此我想明确地使其无效.例如将 NULL 分配给一个指针以使其无效,我只想在迭代器上做同样的事情.container.end() 在这里不是准确的想法.我试图将 NULL 分配给我的迭代器,但它失败了.如何在迭代器上获得与 NULL 指针相同的行为?

Since I know an iterator in the program could be invalidated by some previous operation, I want to invalidate it explicitly. Such as assign NULL to a pointer to invalidate it, I just want to do the same on iterator. container.end() could not be the precise idea here. I tried to assign NULL to my iterator, but it failed. How can I get the same behavior of NULL pointer on iterator?

推荐答案

这不应该一直有效吗?(迭代器总是可赋值和默认构造的)

Shouldn't this always work? (iterators are always assignable and default-constructible)

template <typename Iter>
void foo(Iter it){
  it = Iter(); // invalidate
}

这篇关于如何使迭代器无效?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆