Testing for the beginning of a vector

C

Comp1597

Suppose I have a function with a parameter of type
vector<int>::iterator
How can I test whether that iterator is at the beginning of a vector?
If i denotes the vector<int>::iterator parameter, I could be tempted
to say if (--i == 0) However I believe it's an error to
decrement an iterator before the beginning of a vector.

There's probably a solution involving copying the iterator to a
reverse_iterator but I'm probably missing something really simple.
 
D

Default User

Suppose I have a function with a parameter of type
vector<int>::iterator
How can I test whether that iterator is at the beginning of a vector?
If i denotes the vector<int>::iterator parameter, I could be tempted
to say if (--i == 0) However I believe it's an error to
decrement an iterator before the beginning of a vector.

There's probably a solution involving copying the iterator to a
reverse_iterator but I'm probably missing something really simple.

Compare it to the iterator returned by the begin() member function of
std::vector.





Brian
 
R

red floyd

Suppose I have a function with a parameter of type
vector<int>::iterator
How can I test whether that iterator is at the beginning of a vector?

You can't. Not without comparing it to some specific vector's begin
().
 
M

Michael DOUBEZ

Suppose I have a function with a parameter of type
vector<int>::iterator
How can I test whether that iterator is at the beginning of a vector?
If i denotes the vector<int>::iterator parameter, I could be tempted
to say if (--i == 0) However I believe it's an error to
decrement an iterator before the beginning of a vector.

It is, even reverse iterators uses the n-1 iterator because there is no
guarantee there is a valid pointer before the beginning of an array.
There's probably a solution involving copying the iterator to a
reverse_iterator but I'm probably missing something really simple.

None that I see.

The point is that, if you are using iterators as input, it means you are
working on range, not on the container. In this regard, your question
doesn't make sense.
 
M

mzdude

Suppose I have a function with a parameter of type
vector<int>::iterator
How can I test whether that iterator is at the beginning of a vector?
If i denotes the vector<int>::iterator parameter, I could be tempted
to say     if (--i == 0)    However I believe it's an error to
decrement an iterator before the beginning of a vector.

There's probably a solution involving copying the iterator to a
reverse_iterator but I'm probably missing something really simple.

assuming
std::vector<int> v;
std::vector<int>::iterator i;
.....

if( i == v.begin() )
// i is at the start of the vector

vector<int>::difference_type dif = std::distance( v.begin(), i );
if( dif == 0 )
// i is a the start of the vector
// otherwise it tells how far from the start i really is.
 
V

Victor Bazarov

mzdude said:
assuming
std::vector<int> v;
std::vector<int>::iterator i;
....

if( i == v.begin() )
// i is at the start of the vector

vector<int>::difference_type dif = std::distance( v.begin(), i );
if( dif == 0 )
// i is a the start of the vector
// otherwise it tells how far from the start i really is.

The problem, however, can present itself if 'i' is not an iterator in
'v' at all, because then comparing 'i' with any iterator in 'v' (like
the one returned by 'begin()') or passing them to 'std::distance', might
actually be undefined behavior. IIRC.

V
 
A

Andrey Tarasevich

Suppose I have a function with a parameter of type
vector<int>::iterator
How can I test whether that iterator is at the beginning of a vector?

Without the vector itself? You don't. It can't be done.
If i denotes the vector<int>::iterator parameter, I could be tempted
to say if (--i == 0)

And what would that supposedly mean? What makes you think that
decremented 'begin()' iterator of 'vector said:
However I believe it's an error to
decrement an iterator before the beginning of a vector.

Yes, it is undefined behavior. But again, assuming we disregard that,
what is the logic behind 'if (--i == 0)'?
There's probably a solution involving copying the iterator to a
reverse_iterator but I'm probably missing something really simple.

Well, the "really simple" solution would be to compare 'i' to the
'begin()' of the corresponding vector. Without access to the vector
itself it just can't be done, reverse_iterator or not.
 
P

peter koch

The problem, however, can present itself if 'i' is not an iterator in
'v' at all, because then comparing 'i' with any iterator in 'v' (like
the one returned by 'begin()') or passing them to 'std::distance', might
actually be undefined behavior.  IIRC.
You are right. What could be done is comparing &(*i) with &v[0]. But I
understand the original posters question as if you can determine
whether an iterator points to the first element of any given vector in
your program. This certainly is impossible.

/Peter
 

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,995
Messages
2,570,228
Members
46,818
Latest member
SapanaCarpetStudio

Latest Threads

Top