groupBy C++ function