我可以针对 null 检查 C++ 迭代器吗?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/41352941/
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
Can I check a C++ iterator against null?
提问by Zebrafish
I'm having trouble with vector iterators. I've read in a few places that checking for null iterators isn't possible, and that the usual way to check iterators is to check it against vector.end() after a search. So for example:
我在使用向量迭代器时遇到了麻烦。我在一些地方读到过,检查空迭代器是不可能的,检查迭代器的常用方法是在搜索后根据 vector.end() 进行检查。例如:
vector< Animal* > animalList;
vector<Animal*>::iterator findInList(const type_info& type)
{
// Loop through list of Animals, if Dog found, return iterator to it
}
auto it = findInList(typeid(Dog));
// With a pointer I can check if it's null, but with an iterator I have to check against animalList.end();
The problem is that the container could be empty. With an iterator I can't return null to indicate the container's empty or the search failed. I can return vector::end(), but cplusplus.com says:
问题是容器可能是空的。使用迭代器,我无法返回 null 来指示容器为空或搜索失败。我可以返回 vector::end(),但是 cplusplus.com 说:
If the container is empty, vector::end() function returns the same as vector::begin()
and then for vector::begin() it says:
然后对于 vector::begin() 它说:
If the container is empty, the returned iterator value shall not be dereferenced.
So if I have an empty container, vector::end() and vector::begin() point to the same place, I don't think I can dereference it, and I'm not even sure it's pointing to allocated memory.
所以如果我有一个空容器,vector::end() 和 vector::begin() 指向同一个地方,我认为我不能取消引用它,我什至不确定它指向分配的内存。
Edit: Thanks to everyone. As you have iterated out, vector::end() or vector::begin() do not dereference the iterator, I can safely check against vector::end().
编辑:谢谢大家。当您进行迭代时,vector::end() 或 vector::begin() 不会取消引用迭代器,我可以安全地检查 vector::end()。
回答by paweldac
You don't need to check if iterator is null, because it will never be. You need to check if returned iterator is different from containers end()
position. If it is, you can safely dereference iterator by *it
.
您不需要检查迭代器是否为空,因为它永远不会。您需要检查返回的迭代器是否与容器end()
位置不同。如果是,您可以通过 安全地取消引用迭代器*it
。
If the container is empty, the returned iterator value shall not be dereferenced. So if I have an empty container, vector::end() and vector::begin() point to the same place, I don't think I can dereference it, and I'm not even sure it's pointing to allocated memory.
如果容器为空,则不应取消引用返回的迭代器值。所以如果我有一个空容器,vector::end() 和 vector::begin() 指向同一个地方,我认为我不能取消引用它,我什至不确定它指向分配的内存。
No, checking if(myIt != container.end())
it not dereferencing the iterator. Iterator dereference is *myIt
, which means getting value of object, that iterator is pointing to. It's always safe to check iterators to other iterators from the same container, it's unsafe to dereference iterator not pointing to containers elements.
不,检查if(myIt != container.end())
它不是取消引用迭代器。迭代器解引用 is *myIt
,这意味着获取迭代器指向的对象的值。从同一个容器检查迭代器到其他迭代器总是安全的,取消引用不指向容器元素的迭代器是不安全的。
回答by Mikel F
No, you can't check against NULL because it is not a pointer. Return and also check against animalList.end()
. Only when the iterator is not equal to end()
should you dereference it.
不,您不能检查 NULL,因为它不是指针。返回并检查animalList.end()
。只有当迭代器不等于时才end()
应该取消引用它。
回答by Vlad from Moscow
Just check like this
像这样检查
auto it = findInList( someInfo );
if ( it == animalList.end() ) std::cout << "not found";