template< class T, std::size_t N > struct array;
template <typename T, int n> class myArray { public: myArray() {a = new T[n];} ~myArray() {delete[] a;} T& operator[](int i) {return *(a+i);} private: T* a; };
array<int, 5> arr1 = {1, 2, 3, 4, 5};
或者
array<int, 5> arr1{{1, 2, 3, 4, 5}};
int n1 = arr1[0]; int n2 = arr1.at(0);
for(size_t i = 0; i < arr1.size(); ++i) { cout << arr1[0]<< endl; // cout << arr1.at(0)<< endl; }
array<array<int, 3>, 2> arr2d = {11,12,13,21,22,23};
int n3 = arr2d[0][0];
for(size_t i = 0; i < arr2d.size(); ++i) { for(size_t j= 0; j < arr2d[i].size(); ++j) { cout << arr2d[i][j] << '\t'; } cout << endl; }
原文地址:http://blog.csdn.net/xufeng0991/article/details/43191271