//请问怎么更模板化扩展到n维数组?
#include "stdafx.h"
#include <iostream>
#include <memory>
using namespace std;
void EnableMemLeakCheck()
{
int tmpFlag = _CrtSetDbgFlag(_CRTDBG_REPORT_FLAG);
tmpFlag |= _CRTDBG_LEAK_CHECK_DF;
_CrtSetDbgFlag(tmpFlag);
}
template <typename T>
auto DynHeapArr2(T arrType,int m,int n)->shared_ptr<T *> {
shared_ptr<T *>sptr(new T *[m], [m](decltype(new T *[m]) p) {
for (auto i = 0; i < m; i++) {
delete[] * (p + i);
}
delete[] p;
});
for (auto i = 0; i < m; i++) {
sptr.get()[i] = new T[n];
}
return sptr;
};
int main()
{
EnableMemLeakCheck();
auto m = 5;
auto n = 5;
auto val = 'a';
auto arrType = 'a';
auto sptr = DynHeapArr2(arrType,m, n);
auto myArr = sptr.get();
for (auto i = 0; i < m; i++) {
for (auto j = 0; j < n; j++) {
myArr[i][j] = val++;
}
}
for (auto i = 0; i < m; i++) {
for (auto j = 0; j < n; j++) {
cout << myArr[i][j] << " ";
}
cout << endl;
}
system("pause");
return 0;
}