1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
|
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
template<typename T> T haircut( vector<T> &S )
{
if ( S.empty() ) return T{};
T maximum = *max_element( S.begin(), S.end() );
S.erase( remove( S.begin(), S.end(), maximum ), S.end() );
return maximum;
}
template<typename T> ostream & operator << ( ostream &strm, vector<T> &S )
{
for ( auto e : S ) strm << e << " ";
return strm;
}
int main()
{
vector<int> S{ 1, 2, 3, 4, 5, 4, 3, 2, 5, 4, 3, 2, 1, 0 };
cout << "Initial vector: " << S << '\n';
while ( !S.empty() ) cout << "Maximum is " << haircut( S ) << " Vector is now " << S << '\n';
}
| |