How to check whether STL iterator points at anything?(如何检查 STL 迭代器是否指向任何东西?)
问题描述
可能重复:
C++ 检查迭代器是否有效的最佳方法一个>
我想做这样的事情:
std::vector<int>::iterator it;
// /cut/ search for something in vector and point iterator at it.
if(!it) //check whether found
do_something();
但是没有运算符!对于迭代器.如何检查迭代器是否指向任何东西?
But there is no operator! for iterators. How can I check whether iterator points at anything?
推荐答案
你不能.通常的习惯用法是使用容器的结束迭代器作为未找到"标记.这就是 std::find
返回的内容.
You can't. The usual idiom is to use the container's end iterator as a 'not found' marker. This is what std::find
returns.
std::vector<int>::iterator i = std::find(v.begin(), v.end(), 13);
if (i != v.end())
{
// ...
}
对未赋值的迭代器唯一能做的就是给它赋值.
The only thing you can do with an unassigned iterator is assign a value to it.
这篇关于如何检查 STL 迭代器是否指向任何东西?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如何检查 STL 迭代器是否指向任何东西?


- 哪个更快:if (bool) 或 if(int)? 2022-01-01
- 从父 CMakeLists.txt 覆盖 CMake 中的默认选项(...)值 2021-01-01
- 使用 __stdcall & 调用 DLLVS2013 中的 GetProcAddress() 2021-01-01
- GDB 不显示函数名 2022-01-01
- DoEvents 等效于 C++? 2021-01-01
- OpenGL 对象的 RAII 包装器 2021-01-01
- 将函数的返回值分配给引用 C++? 2022-01-01
- XML Schema 到 C++ 类 2022-01-01
- 如何提取 __VA_ARGS__? 2022-01-01
- 将 hdc 内容复制到位图 2022-09-04