The standard library provides std::advance, but that only advances an iterator to a given offset.
Writing that algorithm yourself is pretty trivial:
template<class Iter, class T>
void advance_until(Iter& it, Iter end, T const& delim){
while(it != end && *it != delim)
++it;
}
Or even:
template<class Iter, class T>
void unsafe_advance_until(Iter& it, T const& delim){
while(*it != delim)
++it;
}
Which would more closely model the way std::advance behaves.
Usage example:
std::string s("hello world!");
std::string::iterator it(s.begin());
advance_until(it, s.end(), 'w');
//unsafe_advance_until(it, 'w');
if(it != s.end())
std::cout << *it << '\n'; // prints 'w'
But maybe there’s already something like that in the standard library or Boost, so I thought I’d ask away.
This is commonly known as
std::find():Successive items can be found through a loop: