Go back to Richel Bilderbeek's homepage.
Go back to Richel Bilderbeek's C++ page.
Algorithm to find a repeating sequence in a container.
Prefer algorithm calls over hand-written loops [0] [1] .
{
const std::string s = "abc***def";
/* const */ int n = 3; //Number of repeats //Note: Must leave out const, don't known why
const char c = '*'; //Character to find
//Assume three Kleene stars can be found
const std::string:: const_iterator i = std::search_n ( s.begin(),s.end(),n,c);
assert( i != s.end() );
//Assume four Kleene stars cannot be found
const std::string::const_iterator j = std::search_n ( s.begin(),s.end(),n+1,c);
assert( j == s.end() );
}
[0] Bjarne Stroustrup. The C++ Programming Language (3rd edition). ISBN: 0-201-88954-4. Chapter 18.12.1: 'Prefer algorithms to loops'.
[1] Scott Meyers. Effective STL. ISBN: 0-201-74962-9. Item 43: 'Prefer algorithm calls over hand-written loops'
Go back to Richel Bilderbeek's C++ page.
Go back to Richel Bilderbeek's homepage.