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 迭代器是否指向任何东西?
基础教程推荐
- 如何检查GTK+3.0中的小部件类型? 2022-11-30
- C++结构和函数声明。为什么它不能编译? 2022-11-07
- 如何将 std::pair 的排序 std::list 转换为 std::map 2022-01-01
- 如何通过C程序打开命令提示符Cmd 2022-12-09
- 常量变量在标题中不起作用 2021-01-01
- 在 C++ 中计算滚动/移动平均值 2021-01-01
- 如何在 C++ 中初始化静态常量成员? 2022-01-01
- 这个宏可以转换成函数吗? 2022-01-01
- 我有静态或动态 boost 库吗? 2021-01-01
- 静态库、静态链接动态库和动态链接动态库的 .lib 文件里面是什么? 2021-01-01
