Sometimes I need to pass a C string to a function using the common C++ iterator range interface [first, last)
. Is there a standard C++ iterator class for those cases, or a standard way of doing it without having to copy the string or call strlen()
?
EDIT:
I know I can use a pointer as an iterator, but I would have to know where the string ends, what would require me to call strlen()
.
EDIT2: While I didn't know if such iterator is standardized, I certainly know it is possible. Responding to the sarcastic answers and comments, this is the stub (incomplete, untested):
class CStringIterator
{
public:
CStringIterator(char *str=nullptr):
ptr(str)
{}
bool operator==(const CStringIterator& other) const
{
if(other.ptr) {
return ptr == other.ptr;
} else {
return !*ptr;
}
}
/* ... operator++ and other iterator stuff */
private:
char *ptr;
};
EDIT3: Specifically, I am interested in a forward iterator, because I want to avoid to iterate over the sring twice, when I know the algorithm will only have to do it once.