编写一个在具有m行n列的二维数组各元素中找出最大元和最小元并显示在屏幕上的函数模板,并通过主函数对它进行调用以验证其正确性。

完全没有解题思路
编写一个在具有m行n列的二维数组各元素中找出最大元和最小元并显示在屏幕上的函数模板,并通过主函数对它进行调用以验证其正确性。例如,可设计该函数模板的原型为: template void maxMin (Type *A, int m, int n );

#include <iostream>
#include <algorithm>
#include <iomanip>
#include <random>

using namespace std;

template <typename T>
void maxMin(T *a, int m, int n)
{
    int size = m * n;
    cout << "max: " << *max_element(a, a + size) << ' '
         << "min: " << *min_element(a, a + size)
         << endl;
}

// A better version
template <typename T, int M, int N>
void maxMin2(T (&a)[M][N])
{
    int size = M * N;
    cout << "max: " << *max_element(&a[0][0], &a[0][0] + size) << ' '
         << "min: " << *min_element(&a[0][0], &a[0][0] + size)
         << endl;
}

template <typename T, int M, int N>
ostream &operator<<(ostream &os, T (&a)[M][N])
{
    for (int i = 0; i < M; i++)
    {
        os << "[";
        for (int j = 0; j < N; j++)
            os << setw(3) << a[i][j] << ' ';
        os << "]\n";
    }
    return os;
}

int main()
{
    const int M = 3;
    const int N = 5;
    int a[M][N];
    random_device rd;
    mt19937 gen(rd());
    uniform_int_distribution<> distrib(1, 100);
    generate(&a[0][0], &a[0][0] + M * N, [&]()
             { return distrib(gen); });
    cout << "A=\n"
         << a;
    maxMin(&a[0][0], M, N);
    maxMin2(a);
    return 0;
}
$ g++ -Wall main.cpp
$ ./a.out
A=
[ 30  38  34  25 100 ]
[ 90  22  68  27  91 ]
[ 46  45  50  46  27 ]
max: 100 min: 22
max: 100 min: 22