find and display sorted v in highest 20th percentile
#include <algorithm>
#include <iostream>
#include <iterator>
#include <vector>
using namespace std;
int main( )
{
const int a[] = { 8, 7, 4, 9, 5, 2, 6, 2, 7,9, };
const int len = sizeof( a ) / sizeof( a[0] );
const int percentile_20 = static_cast<int>( 0.2 * len );
vector<int> v( a, a+len );
copy( a, a+len, v.begin() );
partial_sort( v.begin(), v.begin()+percentile_20,v.end(), greater<int>() );
copy( v.begin(), v.begin()+percentile_20,ostream_iterator<int>( cout, " " ) );
}
Related examples in the same category