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 26 27 28 29 30 31 32 33 34 35
|
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
// Returns biggest n integers of an array as a vector
vector<int> Biggest(int* arr, int arr_size, int n)
{
vector<int> v(arr, arr+arr_size);
sort(v.begin(), v.end(), greater<int>());
v.resize(n);
return v;
}
int main()
{
int arr[] = { 69, 17, 2, 23, 73, 61, 52, 98, 30, 92,
24, 77, 62, 73, 18, 61, 22, 27, 41, 49 };
int arr_size = sizeof(arr)/sizeof(arr[0]);
cout << "Initial array:" << endl;
for (auto& x : arr )
cout << x << " ";
cout << endl << endl;
int n = 5;
cout << "Biggest " << n << " integers:" << endl;
auto v = Biggest(arr, arr_size, n);
for (auto& x : v)
cout << x << " ";
cout << endl;
return 0;
}
|