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 迭代器是否指向任何东西?
基础教程推荐
- c++ STL设置差异 2022-01-01
- 提升 ASIO 流缓冲 2021-01-01
- C++:获取传递给函数的多维数组的行大小 2021-01-01
- 如何部分禁用 cmake C/C++ 自定义编译器检查 2021-01-01
- 如何更改 SysDateTimePick32 或 CDateTimeCtrl 的背景颜色? 2022-01-01
- 将不可复制的闭包对象传递给 std::function 参数 2021-01-01
- 与 CAS 的原子交换(使用 gcc 同步内置函数) 2022-01-01
- 随机插入/删除的综合向量与链表基准 2022-01-01
- 如何在 C++ 中正确使用命名空间? 2022-01-01
- 为什么我们不能使用“虚拟继承"?在 COM 中? 2022-01-01
