NULL iterator?

Dec 8, 2019 at 3:15am
I'm not a beginner, but I have this beginner question:

When I'm using pointers I can initialize them to NULL.

I can then perform operations that may, or may not assign values to them.

When the operations are finished I can query these pointers, and if they are not NULL I know they have something I can use.

Is there an analogous method to know if an iterator has been assigned a value?

I just tried wrapping an operation on one in a try - catch clause with no joy.

Any cleaver work around, beside pairing them with flags?
Dec 8, 2019 at 3:51am
A range always has a sentinel - which you can obtain with std::end(range).

The key is that (assuming the sentinel is an iterator), the sentinel refers to the element one-past-the-end of the range.

Can you use the sentinel?
Last edited on Dec 8, 2019 at 3:52am
Dec 8, 2019 at 2:37pm
Thanks mbozzi:

It looks like it's time for me to bite the bullet and learn modern C++. I don't understand your suggestion.

Can you recommend a good resource to bring someone using paleolithic C++ into the 21st century?
Last edited on Dec 8, 2019 at 2:37pm
Dec 8, 2019 at 3:00pm
> When the operations are finished I can query these pointers, and if they are not NULL
> I know they have something I can use.

If the iterator is not an iterator to the end (to the element after the last element) of the sequence,
it 'points' to a value that we can use.

For example:
1
2
3
4
5
6
std::vector<int> seq { 1, 56, 82 /* etc. */ } ;

// try to find the first occurrence of 99 in the sequence
const auto iter = std::find( seq.begin(), seq.end(), 99 ) ; 

if( iter != seq.end() ) { /* found it */ }


An introductory tutorial: https://cal-linux.com/tutorials/STL.html
Dec 8, 2019 at 3:55pm
Thanks JLBorges:

The problem is that the container might, or might not, be created elsewhere.

I have the iterator, but I don't know anything about the container.

If I was passing a pointer, I could tell it was pointing to something valid if it's not NULL.

This is a big "can't do that" for iterators in ancient C++. I'm wondering if any of the updates to C++ offer a method for me to know if the iterator can be referenced.

It would be nice if I could make the reference in a try - catch block. That would tell me if it's valid. Instead I'm getting a system level exception that the catch clause can't grab.

There's a new C++, and I need to get to know what it can and can't do.
Last edited on Dec 8, 2019 at 3:57pm
Dec 8, 2019 at 6:29pm
If you're only given an iterator and have no idea what the end iterator of the object is, no there's no way to do that. You have to rethink your design. Pretty much any <algorithm> in the standard library requires a begin iterator and an end iterator.

(Also, I would personally disagree very much with using try-catch/exceptions as a form of logic flow. At least, only use it as a final resort.)

There is no general solution to everything; the way you refactor your code, if you choose to do so, will be dependent on your data you have to handle. If you gave a more concrete example of the issue (the actual problem, not specifically the use of NULL/iterators), maybe someone here could offer an alternative.

Personally, I don't use iterators that often in my own code. For-each loops were added to the language in C++11, e.g. "for (auto thing : things)", are very nice to have. Mostly I just use standard containers though; custom containers might need to define their own iterators, if you want them to mesh well with standard algorithms.
Last edited on Dec 8, 2019 at 6:58pm
Dec 8, 2019 at 8:03pm
Thanks Ganado:

It's slowly dawning on me.

Why have an iterator that doesn't iterate?

Is there any way to derive a pointer to a container from an iterator?

Anyway, thanks. I see a flaw in my logic.

I'm still interested in totorials to help move from C++ 98 to C++ 17 or 20.
Dec 9, 2019 at 5:54pm
If I was passing a pointer, I could tell it was pointing to something valid if it's not NULL.

Not really. It's totally possibly to have values stored in pointers that aren't NULL and aren't pointing to valid. In fact, assuming that pointer values must represent actual valid areas of memory that have been allocated for particular uses, is an enormous source of bugs in C and old-fashioned C++ code.

So your iterator situation is no different.
Last edited on Dec 9, 2019 at 5:55pm
Dec 9, 2019 at 7:30pm
Can you recommend a good resource to bring someone using paleolithic C++ into the 21st century?
A Tour of C++ will walk through C++11. That should be enough to make sense of most of the stuff that's been added since:
https://isocpp.org/tour
Dec 9, 2019 at 8:05pm
I'm still interested in totorials to help move from C++ 98 to C++ 17 or 20.

mbozzi has a very good suggestion.

For a from the ground up tutorial there is:

https://www.learncpp.com/

A quick overview of iterators:

https://www.learncpp.com/cpp-tutorial/16-3-stl-iterators-overview/
Topic archived. No new replies allowed.