I do not know of any utility that does directly show all the repeated ones.
But it is possible to do it without loops. Using recursion.
First we will get the maximum with a dameMax
function.
#include <cstdlib>
#include <list>
#include<stdexcept>
#include<iostream>
using namespace std;
int dameMaxRecursivo( int max, std::list<pair<int, int>>::const_iterator it,
std::list<pair<int,int>>::const_iterator end )
{
if ( it==end )
return max;
if ( it->second>max )
return dameMaxRecursivo( it->second, ++it, end );
else
return dameMaxRecursivo( max, ++it, end );
}
int dameMax( const std::list<pair<int, int>>& lista )
{
if ( lista.size()==0 )
throw invalid_argument("dameMax requiere una lista no vacia.");
auto it = lista.begin();
int max = it->second;
++it;
return dameMaxRecursivo( max, it, lista.end() );
}
dameMax checks that the list is not empty and calls the recursive function by passing the maximum until now (the value of the first element in the list) and an iterator to the next element. It also passes iterator at the end so that the recursive function knows when to stop.
First the recursive function checks if it has reached the end. If you do not compare the maximum so far with the element in the received iterator. If it is greater it calls itself with this new value that is greater and with the iterator pointing to the next element, if it is not greater it calls itself with the maximum until now and also with the iterator pointing to the next element.
To print without loops we use the same technique:
void mostarMayoresRecursivo( int max, std::list<pair<int,
int>>::const_iterator it,
std::list<pair<int,int>>::const_iterator end)
{
if ( it==end )
return;
if ( it->second == max )
std::cout<<"Alumno " << it->first << ":" << it->second << std::endl;
mostarMayoresRecursivo( max, ++it, end );
}
void mostrarMayores( const std::list<pair<int, int>>& lista )
{
if ( lista.size()==0 )
return;
int max = dameMax( lista );
mostarMayoresRecursivo( max, lista.begin(), lista.end() );
}
A seasoned reader will notice that there are still loops. It is quite possible that the < < for streams is implemented with some loop. Of course this is also possible to implement without loops using the same technique.
The recursion used, which is tail recursion, can be optimized by some compilers to a loop. But it is also possible to force the compiler not to do it.
And it is possible that system calls to print on screen use a buffer mechanism and end up printing in a loop even though you pass the characters to them one by one without loops. But this is already something external to your program.
That this can be done does not mean that it is advisable. There are cases in which it is recommended, it is a common technique in functional programming.