Carray and Its Derived classes
1.Introduction: The access method and efficiency are the same as those of ordinary arrays. The powerful functions of ordinary arrays are as follows:The array size can be changed.. Array usesQueue-based Data StorageTherefore, the internal data elements are physically arranged in order, so the retrieval and sequential execution of functions such as getat () are quite fast. However, since the length of each queue changes, data must be re-applied for memory, copied memory, and released, the insert/Add/removeat () operation is slow.
Features: The index (array subscript) can be used for fast access, but the insert/delete operation is slow because the element needs to be moved during the insert/delete operation.
Access Method: Through Index access, how can a common array be used.
2. detailed instructions on Using MFC array carray
The arrays supported by the MFC array class are similar to regular arrays.Store any data type. Before using the regular array, you must define it to accommodate all the elements that may be needed, that is, determine the size, and the object created by the MFC array class can be as neededDynamically increase or decreaseThe starting subscript of the array is 0, and the upper limit can be fixed. It can also be increased with the increase of elements, and the address of the array in the memory is still continuously allocated.
MFC defines the array template class carray, and defines cbytearray, carray, cuintarray, cdarray, cstringarray, cobarray, and cptrarray for various common variable types.
CarrayPassTemplate ClassParameter type settingVarious Types
Cbytearray 8-bit unsigned integer byte type
Carray 16-bit unsigned integer word type
Cdarray 32-bit unsigned integer DWORD type
Cuintarray 32-bit unsigned integer uint type
Cstringarray cstring string
Cobarray cobject class and its derived class
Cptrarray void * type pointer
Carray
Cobject
└ Carray
Template <class type, class arg_type> class carray: Public cobject
Parameters:
TypeThe template parameter specifies the type of objects stored in the array. Type is a parameter returned by carray.
Arg_typeThe template parameter specifies the parameter type used to access objects stored in the array. It is usually a reference for type. Arg_type is a parameter passed to carray.
Example 1:Carray <cpoint, cpoint &> m_array;
Example 2: Carray <int, int> myarray; // parameters are required for basic types such as int, Char, and float.
Note:
The carray class supports arrays similar to carray, but can be dynamically compressed and expanded if necessary. The array index starts from 0. You can decide whether to fix the upper bound of the array or allow the current boundary to be extended when an element is added. The upper bound of the memory pair is the continuous allocation of space, and some elements can be empty. Like carray, the access time of the carray index element remains unchanged, regardless of the array size.
Tip:
Before using an array, use setsize to create its size and allocate memory for it. If you do not use
Setsize, adding elements to the array will cause frequent reallocation and copying. Frequent reallocation and copying
Bay is not only inefficient, but also causes memory fragmentation.
".
# Include <afxtempl. h>
Members of the carray class
Constructor
Carray
Construct an empty array attribute
Getsize
Returns the number of elements in the array.
Getupperbound
Returns the maximum valid index value.
Setsize
Set the number of elements contained in this array
Operation
Freeextra
Release unused memory greater than the current upper limit
Removeall
Access from all element elements from this array
Getat
Returns the value of a given index.
Setat
Sets the value of a given index. arrays cannot be expanded.
Elementat
Returns a temporary reference to the element pointer in the array.
Getdata
Allow access to elements in the array. Can be a null extended array
Setatgrow
Set a value for a given index. If necessary, extend the array.
Add
Add elements at the end of the array. If necessary, extend the array.
Append
Append another array to the array. If necessary, extend the array.
Copy
Copy another array to the array. If necessary, extend the array.
Insert/remove
Insertat
Insert an element (or all elements in another array) to the specified index)
Removeat
Removes an element from a specified index.
Operator
OPERATOR []
Set or retrieve elements on a specific index
Carray application example: source code: http://download.csdn.net/detail/nuptboyzhb/4211672
1. The new key is a dialog box-based application named carraytest. In the carraytestdlg. h file, add the carray header file # include <afxtempl. h>
2. Add a member variable to the dialog box:
Carray <cpoint, cpoint &> m_poinyarray; // Save the cpoint Array
3. initialize the array size and values in the array in the oninitdialog () function of the dialog box.
Int m_point_num = 200;
M_poinyarray.setsize (m_point_num );
For (INT I = 0; I <m_point_num; I ++)
{
Cpoint temppt;
Temppt. x = I;
Temppt. Y = I * 10;
M_poinyarray.setat (I, temppt );
}
// That is, the abscissa of a vertex is the same as the index value, and the ordinate of a vertex is the back of the index value;
4. Add the following controls for the dialog box (For details, refer to the source code)
4.1 associate the edit box with an integer variable Index
4.2 compile the message response functions of 10 buttons respectively
4.2.1 obtain
4.2.2 Delete
4.2.3 insert
4.2.4 settings
4.2.5 setatgrow
4.2.6 getsize
4.2.7 getupperbound
4.2.8 add
4.2.9 output all vertices
4.2.10 delete all vertices
The buttons above basically cover most member functions of the carray class;
Serializing carray
Carray does not need to be serialized. You can directly call the serialize function of the parent class. For example:
Void cgraduationdoc: serialize (carchive & AR)
{
If (AR. isstoring ())
{
// Todo: Add storing code here
M_obarray.serialize (AR );
}
Else
{
// Todo: Add loading code here
M_obarray.serialize (AR );
}
}
Note:Carray member type. If it is not a common serializable type, you must implement your own serializability!