> 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
constauto iter = std::find( seq.begin(), seq.end(), 99 ) ;
if( iter != seq.end() ) { /* found it */ }
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.
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.
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.