I’ve watched a few of the Going Native 2013 videos now and, whilst I was impressed by Herb Sutter’s games written in Cinder, I particularly like the slide and gather algorithms demonstrated by Sean Parent in his C++ seasoning presentation:
template<typename Iter>
auto slide(Iter begin, Iter end, Iter target) -> std::pair<Iter,Iter>
{
if (target < begin) return std::make_pair( target, std::rotate(target, begin, end) );
if (end < target) return std::make_pair( std::rotate( begin, end, target ), target );
return std::make_pair( begin, end );
}
template<typename Iter, typename Pred>
auto gather(Iter begin, Iter end, Iter target, Pred predicate) -> std::pair<Iter,Iter>
{
auto start = std::stable_partition( begin, target, std::not1(predicate) );
auto finish = std::stable_partition( target, end, predicate );
return std::make_pair(start, finish);
}
Whilst I haven’t used std::rotate or std::stable_partition, I’m sure to use these two algorithms built on top of them, probably because their behaviour is so much easier to describe.