迭代器的默认值是什么?
对于我正在使用的任何 STL 容器,如果我使用迭代器的默认构造函数声明一个迭代器(此特定容器类型),则迭代器将被初始化为什么?
例如,我有:
std::list<void*> address_list;
std::list<void*>::iterator iter;
iter 将初始化为什么?
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?
For example, I have:
std::list<void*> address_list;
std::list<void*>::iterator iter;
What will iter be initialised to?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
按照惯例,容器的“NULL 迭代器”用于指示没有结果,与
container.end()
的结果进行比较。然而,由于默认构造的容器迭代器不与任何特定容器关联,因此它没有好的值。因此,它只是一个未初始化的变量,对其进行的唯一合法操作是为其分配一个有效的迭代器。
对于其他类型的迭代器,情况可能并非如此。例如,在 istream_iterator 的情况下,默认构造的迭代器表示(比较等于)已到达输入流的 EOF 的 istream_iterator。
By convention a "NULL iterator" for containers, which is used to indicate no result, compares equal to the result of
container.end()
.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.
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) anistream_iterator
which has reached the EOF of an input stream.默认构造函数将迭代器初始化为单值:
The default constructor initializes an iterator to a singular value:
迭代器未初始化,就像
int x;
声明一个未初始化的整数一样。它没有正确定义的值。The iterator is not initialized, just as
int x;
declares an integer which isn't initialized. It does not have a properly defined value.