Home > Back-end >  The dynamic array
The dynamic array

Time:11-13

Now realize class basic meet the requirements of a dynamic array ", users can easily use it,

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 high

CodePudding 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;
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:

refer to the second floor early play big play nuclear 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;
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


This I really useless, just seen inside some third-party component source code, generally encounter where you need to use the container, or directly on the STL,,,

By the way, the CB has similar STD: : map container?

CodePudding user response:

TDictionary

CodePudding user response:

Why the eb to make their own class template, and directly in c + + STL is not good?
I see, TDictionary or DynamicArray, is used to dephi, actually,,
This should be a CB relative to the advantage of DELPHI, and can directly use c + + standard library and STL, BOOST,,,

CodePudding user response:

Not for Delphi, the Delphi types, namely originally DynamicArray is a dynamic array of Delphi native type, String is Delphi String type, TDictionary is Delphi class implements, but mapped to CB (Delphi compiler generates for CB. HPP and. Lib), CB can not only use the standard c + +, still can use most of the Delphi, including derived from the Delphi class c + + class (but not derived from the Delphi generic class, but to build a generic class instance), it is the result of a compiler depth fusion,

CodePudding user response:

I think we should compile CB part of independence, but in this case, is can't use the VCL? EB won't write a c + + version of the VCL for CB alone and FMX,,
Actually this work Microsoft can completely in the VC to complete, but now they only care about c #,,,

CodePudding user response:

Alone to write a c + + version of the VCL and FMX need to do a lot of c + + extension, this work was done by Delphi, no need to do it once, and the VCL, FMX 18000 class is more than 20 years of accumulated as a result, rewrite it again is to write the dead ~ ~ ~

CodePudding user response:

I'm not clear the c + + builder and visual c + + in the underlying some of the differences, only know that in the process of development, using c + + as a development language, can you give me a popular science,,,

Another problem is the execution efficiency, by now the latest c + + builder and the latest visual c + +, who generated exe execution speed is quicker? I guess the vc, but how much faster? Just go along while also didn't search on the net search results, is the comparison between the old version

CodePudding user response:

TStringDynArray in c + + Builder is the string dynamic array


TStringDynArray SL;

SL. Length=5;//is 5 lines

String S;

for (int i=0; i {
S +=SL [I] + "|";
}

CodePudding user response:

nullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnull
  • Related