Difference and use: Array, List, Map

xiaoxiao2021-03-06  44

The Collections class mainly includes three forms and their different applications:

Array - Carray, Cobarray, CstringArray, CPTRARRAY, CBYTEARRAY, CWORDARRAY, CDWORDARRAY, CTYPTRARRAY

List - Clist, Coblist, CStringList, Cptrlist

CMAP - CMAP, CTYPTRMAP, CMAPPTRTOPTR, CMAPPTRTOWORD, CMAPWORDTOPTR, CMAPSTRINGTOPTR

Overall comparison:

Attribute class feature memory structure Array See: type * pnewdata = (Type *) in VC Source CARRAY:: TYPE * PNEWDATA = (TYPE *)]; // Copy New Data from OldMemcpy (PNewData, m_pData, m_nSize * sizeof (TYPE)); // construct remaining elementsASSERT (nNewSize> m_nSize); ConstructElements (& pNewData [m_nSize], nNewSize-m_nSize); // get rid of old stuff (note: no destructors called) Delete [] (Byte *) m_pdata; m_pdata = pnewdata; in Removeat (): int nmovecount = m_nsize - (Nindex Ncount); Destructlements (& M_PData [NINDEX], NCOUNT); if (nmovecount) Memmove (& m_pdata [nindex ncount], nmovecount * sizeof (type)); m_nsize - = ncount; It can be seen that Array uses a queue mode to store data, so its internal data element is arranged in a physical manner, so retrieval The speed of the function of getat () is quite fast in order. However, since each queue length changes, the data needs to be reapply, copy memory, release memory, so INSERT / Add / Removeat () is very slow. If the size of the data element you are using is quite large, and the operations of the array are quite complex, frequent (1E4 or more) use INSERTAT / SETAT / Removeat, etc., consider using CLIST instead. A special case is PTRARRAY / CTYPTRARRAY, and its internal data is a data address, not the data itself, so its efficiency is closer to the CLIST. List on VC source CList :: AddTail () can be seen: CNode * pNewNode = NewNode (m_pNodeTail, NULL); pNewNode-> data = newElement; if (m_pNodeTail = NULL!) M_pNodeTail-> pNext = pNewNode; thus employed List Link mesh mode stores data, so when the chain table data changes, only the pointing changes, so even if the data element is very large, the speed of INSERT / Add / Remove is very fast, but because there is no uniform INDEX, so if you want to find an element, you only traverse the entire linked list. Although the use of List is more cumbersome, especially for small-size data, List is more disappeared. This is why CwordArray does not have CWordList, so it should be limited to whether it can be used to store data in most cases. MapMap stores and retrieves data in a Hashing table, including the two-list table between KEY and VALUE and the mapping relationship between them. For example, in odblib is used :: fHtregister () function, use the key = Class name / value = Class and database column internally, only need to know the name of the class when using the class. Corresponding database mapping method.

As can be seen in the CHARACTERISTICS table below, the value of Key is not repetitive, ie, can only be unique (for example, there is no statement of two identical names in the same program in the previous example), and Value can be repeated ( The class of different names can have exactly the same variables and functions). Despite the high-efficiency search mode, a large number of cMap: lookup () functions is still very time consuming, 400 000 lookup () execution time is about 1 second, and the same number of Cobarray :: getat () functions only It takes 0.3 seconds. Application Skills Array1 If in the case you use, the following cases appear: a. The size of the data element is large B. You often need to perform the Add function (for example, when the database is executing the SELECT statement), you need to perform setsize in advance (int nsize) , int NGROWBY) function, determine the approximate size in advance (then can still increase) and the number of memory applications at the time of time, thereby avoiding frequent memory applications, copying, and deleting work frequently. 2 All names of the PTR containing PTR do not delete memory work, you need to do it yourself. 3 If the REMOVEAT () function is involved in the cycle, the loop must start from the tail, otherwise the element may miss the element, an example: ctypedptraray people; for (int i = people.getsize () - 1; i> = 0; I -) {IF (PeopleRay.getat (i) -> name () == "Tom") {delete pearray.getat (i); people.removeat (i); // cy: If you are from i = 0 cycles, then delete I will miss the check of I-1. }}}} List (temporary) MAP1 Since the execution speed of Lookup is slower, if the mapping must be mapped, the address can be obtained and recorded in the first LOOKUP, which can be used directly. Refer to oodblib / coodbquery :: coodbquery (), note m_cpoodbcdmap = :: fHtoodbdmapof (aclass.cdmapname ()); statement completed the above operation. Below is some performance lists and comments in MSDN in the Collections: Choosing a Collection Class chapter.

Collection Shape Features

SHAPEORDERED? INDEXED? INSERT AN ElementSearch for specified ElementDuplicate Elements? ListyesnofastslowyesarayyESBY INTSLOWYESMAPNOBY Keyfastfastno (keys) Yes (VALUES) YES

Characteristics Of MFC Collection Classes

ClassUses C templatesCan be serializedCan be dumpedIs type-safeCArrayYesYes 1Yes 1NoCByteArrayNoYesYesYes 3CDWordArrayNoYesYesYes 3CListYesYes 1Yes 1NoCMapYesYes 1Yes 1NoCMapPtrToPtrNoNoYesNoCMapPtrToWordNoNoYesNoCMapStringToObNoYesYesNoCMapStringToPtrNoNoYesNoCMapStringToStringNoYesYesYes 3CMapWordToObNoYesYesNoCMapWordToPtrNoNoYesNoCObArrayNoYesYesNoCObListNoYesYesNoCPtrArrayNoNoYesNoCPtrListNoNoYesNoCStringArrayNoYesYesYes 3CStringListNoYesYesYes 3CTypedPtrArrayYesDepends 2YesYesCTypedPtrListYesDepends 2YesYesCTypedPtrMapYesDepends 2YesYesCUIntArrayNoNoYesYes 3CWordArrayNoYesYesYes 31. To serialize, you must explicitly call the collection object's

Serialize Function; To Dump, You Must Explicitly Call ITS

Dump function. You cannot USE The Form

Ar << Collobj to Serialize or the Form

DMP

<< Collobj to Dump.

2. Serializability depends on the underlying collection type For example, if a typed pointer array is based on CObArray, it is serializable;. If based on CPtrArray, it is not serializable In general, the "Ptr" classes can not be serialized..

3. If marked Yes in this column, a nontemplate collection class is type-safe provided you use it as intended. For example, if you store bytes in a CByteArray, the array is type-safe. But if you use it to store characters , ITS Type Safety Is Less Certain.

转载请注明原文地址:https://www.9cbs.com/read-77453.html

New Post(0)