c++ algorithm