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++ 中处理或避免堆栈溢出 2022-01-01
- 您如何将 CreateThread 用于属于类成员的函数? 2021-01-01
- C++,'if' 表达式中的变量声明 2021-01-01
- C++ 程序在执行 std::string 分配时总是崩溃 2022-01-01
- 如何定义双括号/双迭代器运算符,类似于向量的向量? 2022-01-01
- 设计字符串本地化的最佳方法 2022-01-01
- C++ 标准:取消引用 NULL 指针以获取引用? 2021-01-01
- 调用std::Package_TASK::Get_Future()时可能出现争用情况 2022-12-17
- 什么是T&&(双与号)在 C++11 中是什么意思? 2022-11-04
- 运算符重载的基本规则和习语是什么? 2022-10-31