本文介绍了检查迭代器是否有效的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
有任何方法来检查一个迭代器(无论是从向量,列表,deque ...)是(仍然)dereferencable,即没有被无效?
Is there any way to check if an iterator (whether it is from a vector, a list, a deque...) is (still) dereferencable, i.e. has not been invalidated?
我一直使用尝试
- catch
I have been using try
-catch
, but is there a more direct way to do this?
示例:(不起作用)
list<int> l;
for (i = 1; i<10; i++) {
l.push_back(i * 10);
}
itd = l.begin();
itd++;
if (something) {
l.erase(itd);
}
/* now, in other place.. check if itd points to somewhere meaningful */
if (itd != l.end())
{
// blablabla
}
推荐答案
我假设你的意思是是一个迭代器有效,它没有因为容器的更改而失效(例如,向/从向量插入/删除)。在这种情况下,不能,你不能确定一个迭代器是否(安全地)可解除。
I assume you mean "is an iterator valid," that it hasn't been invalidated due to changes to the container (e.g., inserting/erasing to/from a vector). In that case, no, you cannot determine if an iterator is (safely) dereferencable.
这篇关于检查迭代器是否有效的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!