Connecting Tech Pros Worldwide Forums | Help | Site Map

stl iterators "null" value

marcwentink@hotmail.com
Guest
 
Posts: n/a
#1: Dec 30 '05
Dear Sirs, Dear Newsgroup,

Imagine I have some function that only gives me an iterator to a
vector, but not the vector itself. Unfortunately this vector can be
empty and the iterator can point to 'non valid'.

Yes I know this is rather silly, but I did not write the code...

Normally I know you would check for "iterator != vector.end()", but
since I do not seem to have the vector without changing a lot of old
code, I wonder what to do. Comparing the iterator to 0 does not seem to
be acceptable for the compiler.

My thanks in advance, Marc


John Carson
Guest
 
Posts: n/a
#2: Dec 30 '05

re: stl iterators "null" value


<marcwentink@hotmail.com> wrote in message
news:1135934580.591894.29230@z14g2000cwz.googlegro ups.com[color=blue]
> Dear Sirs, Dear Newsgroup,
>
> Imagine I have some function that only gives me an iterator to a
> vector, but not the vector itself. Unfortunately this vector can be
> empty and the iterator can point to 'non valid'.
>
> Yes I know this is rather silly, but I did not write the code...
>
> Normally I know you would check for "iterator != vector.end()", but
> since I do not seem to have the vector without changing a lot of old
> code, I wonder what to do. Comparing the iterator to 0 does not seem
> to be acceptable for the compiler.
>
> My thanks in advance, Marc[/color]

I think this is a dreadful design and personally I would be in favour of
"changing a lot of old code".

If that is not possible, then I think you will have to accept a non-portable
solution. Looking at the implementation on VC++, an iterator that points to
an empty vector has an internal pointer that equals zero. You can get this
pointer using operator->(). Checking to see if this is zero gives the
desired result. For example:

#include <vector>
#include <iostream>

using namespace std;


int main()
{
vector<int> vec;
vector<int>::iterator it = vec.begin();

if(it.operator->() == 0)
cout << "empty\n";
else
cout << "not empty\n";

return 0;
}


--
John Carson


marcwentink@hotmail.com
Guest
 
Posts: n/a
#3: Dec 30 '05

re: stl iterators "null" value


John:
[color=blue]
> I think this is a dreadful design and personally I would be in favour of
> "changing a lot of old code".[/color]

I have to agree with you here, and will find a way to get the vector.
Using that internal pointer is rather uncommon code, and a future
modifier of this code will probably not understand it.

Making the whole thing clearer is the best option, although an quick
solution would have been nice.

Greg
Guest
 
Posts: n/a
#4: Dec 30 '05

re: stl iterators "null" value


marcwentink@hotmail.com wrote:[color=blue]
> Dear Sirs, Dear Newsgroup,
>
> Imagine I have some function that only gives me an iterator to a
> vector, but not the vector itself. Unfortunately this vector can be
> empty and the iterator can point to 'non valid'.
>
> Yes I know this is rather silly, but I did not write the code...
>
> Normally I know you would check for "iterator != vector.end()", but
> since I do not seem to have the vector without changing a lot of old
> code, I wonder what to do. Comparing the iterator to 0 does not seem to
> be acceptable for the compiler.[/color]

The function could not be returning an "iterator" from the client's
point of view, since without knowing how many items are in the
container, the value returned could not be used to iterate the contents
of a container safely.

The function must be returning some kind of reference to an item stored
in a container - the reference could be a pointer to the item itself or
it could even be a pointer to an iterator for the item (which seems to
be the case here).

Since it is possible for the function to return no item, the function
result must be in the form of a pointer (either to the item itself or
to its iterator). The latter approach just adds an extra, unnecessary
layer of indirection. But given the constraints described, the function
has to return a pointer to something (from which the item can be
obtained) in order to allow the function to return NULL (for those
occasions when the function has no item to return).

Greg

marcwentink@hotmail.com
Guest
 
Posts: n/a
#5: Dec 30 '05

re: stl iterators "null" value


Greg:
[color=blue]
> The function could not be returning an "iterator" from the client's
> point of view, since without knowing how many items are in the
> container, the value returned could not be used to iterate the contents
> of a container safely.[/color]

I already solved the problem, but to clarify. The iterator was written
such that it always returned exactly one item. Now other code has
changed and the iterator can point to nothing. But now I got another
function that gives me the vector, so I neatly compare to ...->end();

Michiel.Salters@tomtom.com
Guest
 
Posts: n/a
#6: Jan 2 '06

re: stl iterators "null" value



marcwentink@hotmail.com wrote:[color=blue]
> Dear Sirs, Dear Newsgroup,
>
> Imagine I have some function that only gives me an iterator to a
> vector, but not the vector itself. Unfortunately this vector can be
> empty and the iterator can point to 'non valid'.[/color]

No, it can't.
However, I fail to see the probelm. So what if a vector is empty?
It still has a .begin() and an .end(). They just happen to be the same.
Obviously, since you can't read from .end() this implies you can't
read from .begin() either. Makes perfect sense for an empty vector.
Returning such an iterator is also perfecly legal.
[color=blue]
> Normally I know you would check for "iterator != vector.end()", but
> since I do not seem to have the vector without changing a lot of old
> code, I wonder what to do. Comparing the iterator to 0 does not seem to
> be acceptable for the compiler.[/color]

What use is a vector iterator anyway without .end() ? You can't iterate
with it, and you can't access its value (because it might just be
..end())
So that excludes operator++ and operator*, the two members that are
at the core of the iterator concept.

HTH,
Michiel Salters

marcwentink@hotmail.com
Guest
 
Posts: n/a
#7: Jan 2 '06

re: stl iterators "null" value


> I fail to see the

Dear Michiel, I solved this problem days ago. Don't spend your time on
it.

Closed Thread