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 36 37 38 39 40 41 42 43 44 45 46 47 48 49
|
#include <iostream>
#include <vector>
#include <array>
using std::cout;
using std::vector;
using std::array;
template <class ForwardIterator>
std::vector<ForwardIterator> maxElements(ForwardIterator first, ForwardIterator last)
{
std::vector<ForwardIterator> result;
if (first == last) return result;
ForwardIterator largest = first;
result.push_back(first);
while (++first != last) {
if (*largest < *first) {
result.clear();
largest = first;
result.push_back(first);
} else if (*largest == *first) {
result.push_back(first);
}
}
return result;
}
int
main()
{
std::array<std::array<int,5>, 5> arrays {{
{{2,2,3,4,5}},
{{4,0,2,4,0}},
{{2,2,2,3,3}},
{{2,4,2,3,4}},
{{3,3,2,2,3}}
}};
for (unsigned i=0; i<5; ++i) {
std::vector<array<int,5>::iterator> maxVals = maxElements(arrays[i].begin(), arrays[i].end());
cout << "Max values at ";
for (auto & p : maxVals) {
cout << p-arrays[i].begin()<< ' ';
}
cout << '\n';
}
return 0;
}
|