Quick question: Getting an iterator to the last element

J

Juha Nieminen

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?
 
C

Claudio A. Andreoni

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
 
I

Ian Collins

Juha said:
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(), assuming the container supports reverse iterators.
 
J

Juha Nieminen

Ian said:
rbegin(), assuming the container supports reverse iterators.

But if I need an iterator, not a reverse iterator?

std::list<int>::iterator iter = l.rbegin();

error: conversion from
`std::reverse_iterator<std::_List_iterator<int, int&, int*> >'
to non-scalar type
`std::_List_iterator<int, int&, int*>' requested
 
?

=?ISO-8859-1?Q?Erik_Wikstr=F6m?=

But if I need an iterator, not a reverse iterator?

std::list<int>::iterator iter = l.rbegin();

error: conversion from
`std::reverse_iterator<std::_List_iterator<int, int&, int*> >'
to non-scalar type
`std::_List_iterator<int, int&, int*>' requested

You can use the base() method to get a normal iterator from a reverse
iterator, notice however that if you can base() on l.rbegin() you get
r.end() so you either have to increment the reverse iterator before
calling base(), or decrement the iterator returned by base() to get an
iterator to the last element.

All of this is quite a lot of extra work however, since --l.end() will
give you an iterator to the last element as long as l.size != 0.

Another thing to notice is that all of this (both using --l.end() and
the reverse-iterator) requires that the container supports bidirectional
iterators (though I'm not aware of any containers that don't).
 
J

Jerry Coffin

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?

container.rbegin() would be one obvious possibility.
 
J

James Kanze

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?

No. Depending on how the iterator is implemented, it may or may
not be legal. (Obviously, it's never legal if the iterator
isn't at least bi-directional.)
Or is there a cleaner way of getting an iterator to the last
element?

The only sure way is:

C::iterator it = container.end() ;
-- it ;

For containers with random access iterators, container.end() - 1
also works.
 
G

Gavin Deane

Isn't that exactly what I suggested in my question?

No. You suggested --container.end() which modifies a temporary - which
is not always legal. The usual example I've seen where your code won't
work is if the iterator is simply a raw pointer.

Gavin Deane
 
J

Juha Nieminen

Gavin said:
No. You suggested --container.end() which modifies a temporary - which
is not always legal. The usual example I've seen where your code won't
work is if the iterator is simply a raw pointer.

Why would your version make any difference?
 
K

Kai-Uwe Bux

Juha said:
Why would your version make any difference?

Consider:

#include <cstddef>
#include <cassert>

template < typename T, std::size_t N >
class array {

T the_data [N];

public:

typedef T* iterator;

array ( T const & value ) {
for ( std::size_t i = 0; i < N; ++i ) {
the_data = value;
}
}

T & operator[] ( std::size_t i ) {
assert( i < N );
return ( the_data );
}

T const & operator[] ( std::size_t i ) const {
assert( i < N );
return ( the_data );
}

std::size_t size ( void ) const {
return ( N );
}

iterator begin ( void ) {
return ( the_data );
}

iterator end ( void ) {
return ( the_data + N );
}

}; // array


typedef array< double, 3 > point3d;

int main ( void ) {
point3d P (0.1);
{
// compile time error:
point3d::iterator before_end = -- P.end();
}
{
// fine:
point3d::iterator before_end = P.end();
-- before_end;
}
}


If you had an implementation of std::vector<> that uses pointers to
implement iterators, you would run into similar problems.


Best

Kai-Uwe Bux
 
M

Michael DOUBEZ

Kai-Uwe Bux a écrit :
Juha said:
Why would your version make any difference?

[snip] code
If you had an implementation of std::vector<> that uses pointers to
implement iterators, you would run into similar problems.

I agree with you, this is buggy behavior.

Just not to be misleading about iterator being pointer, such an
implementation would lack iterator traits.

A minimal Input iterator would be:
struct iterator
{
//traits
typedef T value_type;
typedef ptrdiff_t distance_type;
//etc

//actual pointer
T* pointer;
//constructor
iterator(T* def=NULL):pointer(def){}
//assignment operator
iterator operator=(const iterator& it)
{
this->pointer=it.pointer;
return *this;
}
//equality/inequality operator
bool operator==(const iterator& it){return this->pointer==it.pointer;}
bool operator!=(const iterator& it){return this->pointer!=it.pointer;}
//dereference operator
T operator*(){return *(this->pointer);}

//pre/post increment operator
iterator operator++(){++this->pointer;return *this;}
iterator operator++(int){iterator
old(this->pointer);++this->pointer;return old;}
iterator operator--(){--this->pointer;return *this;}
iterator operator--(int){iterator
old(this->pointer);--this->pointer;return old;}
};

And in this case, the example you gave do compile.


Michael.
 
K

Kai-Uwe Bux

Michael said:
Kai-Uwe Bux a écrit :
Juha said:
Gavin Deane wrote:
James Kanze wrote:
The only sure way is:
C::iterator it = container.end() ;
-- it ;
Isn't that exactly what I suggested in my question?
No. You suggested --container.end() which modifies a temporary - which
is not always legal. The usual example I've seen where your code won't
work is if the iterator is simply a raw pointer.
Why would your version make any difference?

[snip] code
If you had an implementation of std::vector<> that uses pointers to
implement iterators, you would run into similar problems.

I agree with you, this is buggy behavior.

Buggy as in "not standard compliant" or buggy as in "I don't like it"?

Just not to be misleading about iterator being pointer, such an
implementation would lack iterator traits.
[snip]

Huh? By [24.3.1/2], std::iterator_traits<> is specialized for any pointer
type T* as:

template<class T>
struct iterator_traits<T*> {
typedef ptrdiff_t difference_type;
typedef T value_type;
typedef T* pointer;
typedef T& reference;
typedef random_access_iterator_tag iterator_category;
};

and for T const * as:

template<class T> struct iterator_traits<const T*> {
typedef ptrdiff_t difference_type;
typedef T value_type;
typedef const T* pointer;
typedef const T& reference;
typedef random_access_iterator_tag iterator_category;
};


I thought, a standard conforming implementation of std::vector<T> could use
T* as iterator and T const * as const_iterator. What is it that I am
missing?


Best

Kai-Uwe Bux
 
P

Pete Becker

Michael said:
Kai-Uwe Bux a écrit :
Juha said:
Gavin Deane wrote:
James Kanze wrote:
The only sure way is:
C::iterator it = container.end() ;
-- it ;
Isn't that exactly what I suggested in my question?
No. You suggested --container.end() which modifies a temporary - which
is not always legal. The usual example I've seen where your code won't
work is if the iterator is simply a raw pointer.
Why would your version make any difference?

[snip] code
If you had an implementation of std::vector<> that uses pointers to
implement iterators, you would run into similar problems.

I agree with you, this is buggy behavior.

Well, buggy in the sense that the user's code is incorrect. Pointers are
valid iterators.
Just not to be misleading about iterator being pointer, such an
implementation would lack iterator traits.

No, you get iterator traits from the iterator_traits<Iter> template
instantiation, not directly from the iterator.

--

-- Pete
Roundhouse Consulting, Ltd. (www.versatilecoding.com)
Author of "The Standard C++ Library Extensions: a Tutorial and
Reference." (www.petebecker.com/tr1book)
 
M

Michael DOUBEZ

Kai-Uwe Bux a écrit :
Michael said:
Kai-Uwe Bux a écrit :
Juha Nieminen wrote:

Gavin Deane wrote:
James Kanze wrote:
The only sure way is:
C::iterator it = container.end() ;
-- it ;
Isn't that exactly what I suggested in my question?
No. You suggested --container.end() which modifies a temporary - which
is not always legal. The usual example I've seen where your code won't
work is if the iterator is simply a raw pointer.
Why would your version make any difference?
[snip] code
If you had an implementation of std::vector<> that uses pointers to
implement iterators, you would run into similar problems.
I agree with you, this is buggy behavior.

Buggy as in "not standard compliant" or buggy as in "I don't like it"?

Just not to be misleading about iterator being pointer, such an
implementation would lack iterator traits.
[snip]

Huh? By [24.3.1/2], std::iterator_traits<> is specialized for any pointer
type T* as:

I thought, a standard conforming implementation of std::vector<T> could use
T* as iterator and T const * as const_iterator. What is it that I am
missing?

Nothing. I missed the point.
Michael
 
J

James Kanze

Why would your version make any difference?

The variable it isn't a temporary.

The version I proposed is guaranteed to work with any
bi-directional iterator. The version you proposed isn't.
That's the difference.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,764
Messages
2,569,567
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top