Say I have some std::string::iterator it for some unknown string str. Is it possible for me to check when it == str.end() without access to str or str.end()?
Say I have some std::string::iterator it for some unknown string str. Is it possible for me to check when it == str.end() without access to str or str.end()?
An iterator is, logically, a pointer. It is not an index and a reference to a container. Therefore, you cannot get information about the container that contains the iterator item.
This is in some sense analogous to the pass-array-as-pointer problem: when you pass an array as a pointer, you also always have to pass the length, because the pointer does not identify where the array ends. Your iterator does not identify where the string ends.
As others have pointed out, a crude approach that would work in most situations is to check if the character pointed to by the iterator is the null character. This is ok, as long as your string doesn't contain any null characters; but remember that if you're dealing with strings as an interface boundary others may pass strings with null in them, because it is perfectly legal to do so.