迭代器的默认值是多少?

What is an iterator#39;s default value?(迭代器的默认值是多少?)
本文介绍了迭代器的默认值是多少?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对于我正在使用的任何 STL 容器,如果我使用迭代器的默认构造函数声明一个迭代器(该特定容器类型的),迭代器将被初始化为什么?

For any STL container that I'm using, if I declare an iterator (of this particular container type) using the iterator's default constructor, what will the iterator be initialised to?

例如,我有:

std::list<void*> address_list;
std::list<void*>::iterator iter;

iter 将被初始化为什么?

What will iter be initialised to?

推荐答案

按照约定,容器的NULL迭代器",用于表示没有结果,比较等于container.end().

By convention a "NULL iterator" for containers, which is used to indicate no result, compares equal to the result of container.end().

 std::vector<X>::iterator iter = std::find(my_vec.begin(), my_vec.end(), x);
 if (iter == my_vec.end()) {
     //no result found; iter points to "nothing"
 }

但是,由于默认构造的容器迭代器不与任何特定容器相关联,因此它没有什么好的价值.因此它只是一个未初始化的变量,唯一合法的操作就是为其分配一个有效的迭代器.

However, since a default-constructed container iterator is not associated with any particular container, there is no good value it could take. Therefore it is just an uninitialized variable and the only legal operation to do with it is to assign a valid iterator to it.

 std::vector<X>::iterator iter;  //no particular value
 iter = some_vector.begin();  //iter is now usable

<小时>

对于其他类型的迭代器,这可能不是真的.例如,在 istream_iterator 的情况下,默认构造的迭代器表示(比较等于)已达到输入流 EOF 的 istream_iterator.


For other kinds of iterators this might not be true. E.g in case of istream_iterator, a default-constructed iterator represents (compares equal to) an istream_iterator which has reached the EOF of an input stream.

这篇关于迭代器的默认值是多少?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

Rising edge interrupt triggering multiple times on STM32 Nucleo(在STM32 Nucleo上多次触发上升沿中断)
How to use va_list correctly in a sequence of wrapper functions calls?(如何在一系列包装函数调用中正确使用 va_list?)
OpenGL Perspective Projection Clipping Polygon with Vertex Outside Frustum = Wrong texture mapping?(OpenGL透视投影裁剪多边形,顶点在视锥外=错误的纹理映射?)
How does one properly deserialize a byte array back into an object in C++?(如何正确地将字节数组反序列化回 C++ 中的对象?)
What free tiniest flash file system could you advice for embedded system?(您可以为嵌入式系统推荐什么免费的最小闪存文件系统?)
Volatile member variables vs. volatile object?(易失性成员变量与易失性对象?)