问题描述
是否有一种简单的方法可以将一个迭代器(*不是*反向迭代器)添加到列表中的
最后一个元素? last()函数返回元素本身,而不是
迭代器。
谢谢,
-Howard
Is there an easy way to get an iterator (*not* a reverse-iterator) to the
last element in a list? The last() function returns the element itself, not
an iterator.
Thanks,
-Howard
推荐答案
list.end() - 1或列表怎么样。 start()+(list.size() - 1)
-
Ian Collins。
How about list.end()-1 or list.start()+(list.size()-1)
--
Ian Collins.
list.end() - 1或list.start()+如何? (list.size() - 1)
How about list.end()-1 or list.start()+(list.size()-1)
列表迭代器是双向的,而不是随机访问,所以你不能做
算术像这样。更不用说list.size()是O(n)。
但是你可以这样做:
list< T> :: iterator it = l.end();
if(!l.empty())
--it;
现在它指向最后一个元素,或者,如果列表为空,则结束()。
list iterators are bidirectional, not random access, so you can''t do
arithmetic like this. Not to mention that list.size() is O(n).
But you can do:
list<T>::iterator it = l.end();
if( !l.empty())
--it;
Now "it" points to either the last element or, if the list is empty, end().
list.end() - 1或list.start()+(list.size)怎么样()-1)
How about list.end()-1 or list.start()+(list.size()-1)
列表迭代器是双向的,而不是随机访问,因此你不能这样做
算术。更不用说list.size()是O(n)。
list iterators are bidirectional, not random access, so you can''t do
arithmetic like this. Not to mention that list.size() is O(n).
糟糕,对不起。
-
Ian Collins。
Oops, sorry.
--
Ian Collins.
这篇关于迭代器列表中的最后一个元素?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!