Possible Duplicate:
C++ Best way to check if an iterator is valid
我想做这样的事情:
std::vector<int>::iterator it;
// /cut/ search for something in vector and point iterator at it.
if(!it) //check whether found
do_something();
但是没有运算符!对于迭代器。如何检查迭代器是否指向任何东西?
最佳答案
你不能。通常的习惯用法是使用容器的结束迭代器作为“未找到”标记。这就是 std::find
返回的内容。
std::vector<int>::iterator i = std::find(v.begin(), v.end(), 13);
if (i != v.end())
{
// ...
}
对未赋值的迭代器唯一能做的就是给它赋值。
https://stackoverflow.com/questions/824157/
相关文章:
c++ - Visual Studio 可以在重建前提示吗?
c++ - 有没有办法在 Visual Studio 中获取某些 c++ 函数的调用图?
c++ - #include 在查找预编译头文件时跳过 -- 查找预编译头文件时文件意外结束
c++ - 条件变量 - 为什么在调用 pthread_cond_wait() 之前调用 pthre
c++ - 除以零 : Undefined Behavior or Implementation D
c++ - 如何以跨平台的方式获取(几乎)唯一的系统标识符?