Learning C++ algorithm.