快速问题:获取最后一个元素的迭代器 [英] Quick question: Getting an iterator to the last element

查看:128
本文介绍了快速问题:获取最后一个元素的迭代器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我其实不确定这个:标准是否保证

如果数据容器中至少有一个元素,那么

" - container.end()"将工作并给一个迭代器到容器中的最后一个

元素?


或者有一种更简洁的方法来获取迭代器到最后一个

元素?

I''m actually not sure about this one: Does the standard guarantee
that if there''s at least one element in the data container, then
"--container.end()" will work and give an iterator to the last
element in the container?

Or is there a cleaner way of getting an iterator to the last
element?

推荐答案

嗨!


不,STL不保证。

如果你在一个空容器上调用operator--你得到某种类型的垃圾邮件地址(这可能会导致一个SIGSEGV)。

完全由你来检查容器中是否存在元素

...但是如果有的话,运营商 - 将会很好地完成它的工作。


请记住,如果你有一个空数据容器,那么最后一个迭代器

与第一个相同(在GNU STL上你设置了它们)

到0,如果我记得很清楚的话。

你可能想检查这个条件:x.begin()!= x.end()

或x.size()0 ...


[如果由于某种原因确实喜欢访问最后一个或第一个

元素直接(不通过迭代器)你可能

也在某些容器上使用front()和back()(例如:vector)。

无论如何,如果容器是空的,他们会返回垃圾箱。


再见!

Claudio A. Andreoni

Juha Nieminen写道:
Hi!

No, the STL does not guarantees that.
If you call operator-- on an empty container, you get some kind
of junk address (which will probably cause a SIGSEGV).
It is completely upon you to check for the existence of elements
in the container... but if there is any, operator-- will do its
job well.

Remember, if you have an empty data container, the last iterator
is the same as the first (on the GNU STL you have both them set
to 0, if I remember well).
You might so want to check for this condition: x.begin() != x.end()
or x.size() 0 ...

[If you do prefer for some reason accessing the last or the first
element directly (without passing through an iterator) you might
also use front() and back() on some containers (eg: vector).
They return trash if the container is empty anyway].

Bye!
Claudio A. Andreoni
Juha Nieminen wrote:

我其实不确定这个:标准是否保证

如果至少有一个元素在数据容器,然后

" - container.end()"将工作并给一个迭代器到容器中的最后一个

元素?


或者有一种更简洁的方法来获取迭代器到最后一个

元素?
I''m actually not sure about this one: Does the standard guarantee
that if there''s at least one element in the data container, then
"--container.end()" will work and give an iterator to the last
element in the container?

Or is there a cleaner way of getting an iterator to the last
element?


Juha Nieminen写道:
Juha Nieminen wrote:

我其实不确定这个:标准是否保证

如果数据容器中至少有一个元素,那么

" - container.end()"将工作并给一个迭代器到容器中的最后一个

元素?


或者有一种更简洁的方法来获取迭代器到最后一个

元素?
I''m actually not sure about this one: Does the standard guarantee
that if there''s at least one element in the data container, then
"--container.end()" will work and give an iterator to the last
element in the container?

Or is there a cleaner way of getting an iterator to the last
element?



rbegin(),假设容器支持反向迭代器。


-

Ian柯林斯。

rbegin(), assuming the container supports reverse iterators.

--
Ian Collins.


Claudio A. Andreoni写道:
Claudio A. Andreoni wrote:

如果你在一个空容器上调用operator-- b $ b
If you call operator-- on an empty container



请注意我这样说:

Notice that I said this:


Juha Nieminen写道:
Juha Nieminen wrote:

>如果数据容器中至少有一个元素
>if there''s at least one element in the data container


这篇关于快速问题:获取最后一个元素的迭代器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆