C + + stipulates that once an array is defined, its length cannot be changed; in other words, the array capacity cannot grow or decrease dynamically. Such an array is called a static array. Static arrays are sometimes inconvenient for coding code, and we can implement a dynamic array with a custom array class. The so-called dynamic array, is the exponential group capacity can be used in the process of increasing or decreasing at any time.
The full implementation code for the dynamic array is as follows:
#include <iostream> #include <cstring> #include <cstdlib> using namespace std;
Template<typename T, int n> class array{public:array ();
~array (); Public:t & operator[] (int i); overloaded subscript operator [] int length () const {return m_length;} Gets the array length bool capacity (int n); Changing array capacity Private:int m_length; The current length of the array int m_capacity; The current memory capacity (the number of elements that can be) T *m_p;
Pointer to array memory};
Template<typename T, int n> array<t, N>::array () {m_p = new t[n];
m_capacity = M_length = N; Template<typename T, int n> array<t, N>::~array () {delete[] m_p;} template<typename t, int N> T & Amp Array<t, n>::operator[] (int i) {if (i<0 | | i>=m_length) {cout<< "Exception:array index out of Bounds! "
<<endl;
return m_p[i]; Template<typename T, int n> bool array<t, n>::capacity (int n) {if (n > 0) {//increase array int len = m _length + N; The enlarged array length if (len <= m_capacity) {//The existing memory is sufficient to accommodate the enlarged array m_length = Len;
return true; }else{//Existing memory cannot accommodate the enlarged array t *ptemp = new T[m_length + 2 * n * sizeof (T)]; Increased memory sufficient to hold 2*n elements if (ptemp = NULL) {//Memory allocation failure cout<< exception:failed to allocate me mory! "
<<endl;
return false;
}else{//Memory allocation succeeded memcpy (ptemp, m_p, m_length*sizeof (T));
Delete[] m_p;
m_p = ptemp;
m_capacity = M_length = Len; }else{//shrink array int len = M_length-abs (n); The shrunk array length if (Len < 0) {cout<< "Exception:array length is too small!"
<<endl;
return false;
}else{m_length = len;
return true;
int main () {array<int, 5> arr;
The array element is assigned for (int i=0, len=arr.length (); i<len; i++) {arr[i] = 2*i; }//First timePrint array for (int i=0, len=arr.length (); i<len; i++) {cout<<arr[i]<< "";
} cout<<endl;
Enlarging the capacity and assigning value to the added element arr.capacity (8);
for (int i=5, len=arr.length (); i<len; i++) {arr[i] = 2*i;
//second print array for (int i=0, len=arr.length (); i<len; i++) {cout<<arr[i]<< "";
} cout<<endl;
Shrinkage Capacity arr.capacity (-4);
Third print array for (int i=0, len=arr.length (); i<len; i++) {cout<<arr[i]<< "";
} cout<<endl;
return 0; }Run Result:
0 2 4 6 8
0 2 4 6 8 10 12 14 16 18 20 22-24
0 2 4 6 8 10 12 14 16
Array is a class template that has a type parameter T and a n,t parameter that indicates the type of the array element, and N indicates the length of the array.
The capacity () member function is the key to the array class, which allows the array capacity to increase or decrease dynamically. The size of the array is increased when passed to a positive number, and the size of the array is reduced when it is passed a negative number.
The array element is accessed through [] because the [] operator is overloaded as a member function in the array class, and the return value is a reference to an array element. If you return the value of an array element directly, you cannot assign a value to an array element.