However, the use of the dynamic array also has a problem, for example, when the number of elements in the array changes, happen to the release of memory and the application of operation, this makes the efficiency of operation is very slow, and it's easy to produce memory fragments, imagine if the user is continuously with the push_back () function to increase 10000 elements, how the program is running?
How can you improve it?
One idea is to allocate more memory in advance, and then in quite some time, when the number of elements in the array changes, don't have to reapply memory space, the array can also continue to use, thus improve the efficiency,
For machine algorithm, the contradiction of "time" and "space" is always present: storage, run faster; Less storage, run slowly, and it is here, this kind of treatment is to use the space in time, while on the memory space is "wasted" part of the space, but in a long period of time, as long as the space has not changed, running time complexity is constant, only when the existing space is not enough, there are only memory of a new application for and release of operation, the growth of the most simple way is to double, namely 2 times, so the size of the memory allocation is the power to the power of 2,
Therefore, we add a m_nMax data, used to store the application to the size of the memory space, and m_nSize truly record the number of elements in the array, class interface is as follows:
The class DArray
{
Private:
Double * m_pData;//storage array pointer to dynamic memory
Int m_nSize;//the number of elements in the array
Int m_nMax;//reserved for dynamic array of memory size
Private:
Void Init ();//initialize
Void Free ();//release dynamic memory
The inline int InvalidateIndex (int nIndex);//judge the legitimacy of the subscript
Public:
DArray (); The default constructor//
DArray (int nSize, double dValue=https://bbs.csdn.net/topics/0);//other constructors, the array size must be set, and set all the elements of 0; Of course you can also define other different parameters of the constructor, to facilitate users to use
DArray (const DArray& Arr);//copy constructor (preferably contains dynamic allocation for all members of the class copy constructor)
~ DArray ();//destructors
Void the Print ();//output shows all the value of the array element
Int GetSize ();//get the array size (number of elements)
Int SetSize (int nSize);//set the array size again, note: if the nSize is less than the original array size, can take before the truncation nSize elements as a new array elements; If nSize is greater than the original size of the array, the new element value to set the default value of 0
Double GetAt (int nIndex);//get an element
Double operator (int [] nIndex) const;//reloading the [] operator, in order to like traditional array get element value by a [k]
Int SetAt (int nIndex, double dValue);//set the value of an element
Int PushBack (double dValue);//add a new element to the array at the end of the
Int DeleteAt (int nIndex);//delete an element in an array from
Int InsertAt (int nIndex, double dValue);//insert a new element to the array
DArray & amp; DArray: : operator=(const DArray& Array);//overloaded assignment operation symbol "="
};
Note that only added a data m_nMax here, all the other function interface can not change! For users, is still in accordance with the previously used to use the interface functions to manipulate the dynamic array, but essentially the dynamic array operating efficiency is greatly raised.
Job requirements
Dynamic array to meet the interface (Dynamic array) program, submit the project source files;
Strictly follow the basic programming specification and style;
Experience determines the class interface, its implementation can be different: users only dealing with class (object), the concrete is done by which data is not concerned about
Further understanding and experience the encapsulation of c + + features;
Still use the previous test code, do more sufficient tests, to deal with all kinds of extreme need more code and debugging time,
CodePudding user response:
Go and see the STL: the VECTOR, the way it works is in advance to apply for a part of memory as the standby, efficiency is highCodePudding user response:
CB has a dynamic array (the actual is Delphi dynamic array type), than STD: : vector is convenient, because is the original type of compiler management, like String, there will be no memory leaks:DynamicArray
A.s et_length ((10, 10));
A [1, 1)=1;//A [1] [1]=1; Don't support the
The only problem is that the multidimensional form does not support C multidimensional array of standard style [] []...
But in Derlphi is supported by the
A: [1, 1, 1]=1;
A [1] [1] [1] :=1;//C standard style
: A [1] [1, 1]=1;
A (1, 1] [1] :=1;
Is one thing, DynamicArray only supports the first form is estimated to be lazy, when overloaded [] after all STD: : vector of multidimensional arrays is to support the C standard style
CodePudding user response: