VC + + Basic class [chart]---Dynamic array and dynamic link list

Source: Internet
Author: User

C + + also has a corresponding dynamic array, dynamic linked list, mapping table template class, is the STL: vector, List, map they are part of the C + + standard, for the portability of the program is also good, but in MFC programming using CArray, CList, CMap will be more convenient! The origin of CArray, CList and CMap? ...  ①, the basic description of an array: An array is a fixed-size, sequential collection of elements of the same data type, with each element having a fixed position in the array. Put 10 numbers in the array, assuming that the name of the array is number, can be called the first element in the array is number[0], the second number is number[1] ... where 0, 1~9 is called the subscript of the element, the subscript indicates the relative position of the element in the array.   For example, a regular static array is defined as: int number[20]; CPoint PT[10]; CButton Btn[10];====================================================②, the dynamic array class in MFC is: Carraycarray is a template class, similar to a regular array, However, the length can be dynamically increased; The regular array must first know the number of elements before use, that is, the size is determined, and the MFC dynamic array class CArray created objects can be dynamically increased or decreased as needed, the starting subscript of the array is 0, and the upper limit can be fixed, but also can increase with the element increases, The position of the array in memory is still continuous (this is critical)!   Although CArray is a template class, Microsoft defines each of the common variable types in MFC: CByteArray, CDWordArray, CObArray, CPtrArray, CStringArray, CUIntArray, Cwordrray, etc., so, if you use the data structure in the program has the appropriate types of the above, you can take it directly, no need to repeat the definition, see msdn! ========================================================③, CArray type Object definition: Where the arg_ prefix is the incoming type, and the other is the return type CArray < int, int> arrint; CArray <cpoint, CPoint &> arrpoint; CArray <cbutton, CButton &> arrbutton;//When using the basic type, the second parameter can not be referenced, but the use of class type, such as conforming to the type of the second argument is best to pass the object reference,//This can avoid copy constructor calls, improve efficiency!  cstringarray arrstring; CByteArray Arrbyte;====================================================④, CArray class use://Insert node, delete node, traverse array Int_ptr idx = 0; CArray <cpoint, CPoint &> arrpoint;//arrpoint.setsize (100, 10); BOOL BRet = Arrpoint.isempty (); for (idx = 0; idx < ++idx) { cpoint pt; pt. SetPoint (idx, IDX);  arrpoint.add (PT);//The addition of elements}int ncount = Arrpoint.getcount (); int maxidx = Arrpoint.getupperbound (); bRet = Arrpoint.isempty (); CPoint point = Arrpoint.getat, Arrpoint.removeat (10, 5); The deletion of the specified element ncount = Arrpoint.getcount ();p oint = Arrpoint.getat (Ten);p oint = arrpoint[11]; //The traversal of the array for (idx = 0; idx < Arrpoint.getcount (); ++IDX) { cpoint pt = arrpoint.getat (idx);  pt.x = 123;}  point.x = Point.y = 999;arrpoint.setat (5, point);  for (idx = 0; idx < Arrpoint.getcount (); ++idx) { cpoi NT pt = arrpoint.getat (idx);  pt = pt;}  arrpoint.removeall (); ncount = Arrpoint.getcount (); bRet = Arrpoint.isempty (); ====================================================⑤, CArray Efficiency question: Q: CArray really works, but why is the rate of assignment slow after 100,000 data? A: CArray is very useful, just because you do not specify the maximum size of the array. Therefore, when you add a new element, the class will reallocate space from the heap, and unfortunately, the class will reallocate the space for the array each time the new element is inserted, and if you add many new elements to it, all of these allocations and copies of the array will slow it down.   The way to solve this problem is that you can use the second parameter of the SetSize function to change the frequency of this redistribution. For example, if you set the parameter to 500, it will reallocate and add 500 new space each time the array space is exceeded, instead of 1, so that you can add another 499 element space without redistribution, which also greatly increases the speed of the program.  ====================================================@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@============== A basic description of the ======================================⑥, linked list: A linked list is also a collection of ordered data, but each element contains the address of the next element. Each element consists of two parts: the data and the chain (a pointer to the address of the next element), where only a single linked list is discussed, and each node has only one chain that points to the successor node. A pointer variable is typically used to point to the first element, called the head pointer of the linked list. The last element of the list contains a null pointer that identifies the end of the linked list. A linked list is a complex data structure whose data is interconnected so that the list is divided into three types: single-linked list, circular list, doubly linked list   linked list in memory position is discontinuous (this is very important)!   For example, a normal list node is defined as: struct node{    int num;    struct node *p;}; ================================= The dynamic linked list class in ===================⑦ and MFC is: Clistclist is a doubly linked table, so the elements of the header, tail, and the known position (POSITION) in the table are inserted very fast. By value orIndex lookups require sequential search, but may be slow if the table is long!   Although CArray is a template class, Microsoft in MFC for a variety of commonly used variable types are defined: CPtrList, CObList, cstringlist, etc., so, if you use in the program data structure has the appropriate types of the above, Can be used directly, no need to repeat the definition, see msdn! ====================================================⑧, CList type Object definition: Where the arg_ prefix is the incoming type, the other is the return type CList <int, int > arrint; CList <cpoint, CPoint &> arrpoint; CList <cbutton, CButton &> arrbutton;//when using the basic type, the second argument can be used without reference, but the second argument is best to pass the object's reference when using a class type that conforms to the type,// This avoids the call of the copy constructor and improves efficiency! Use of ====================================================⑨, CList class: int idx = 0; CList <cpoint, CPoint &> ptlist (100); BOOL BRet = Ptlist.isempty (); for (idx = 0; idx < ++idx) { cpoint pt; pt. SetPoint (idx, IDX);  ptlist.addtail (PT);//Add elements to the end of the list}int ncount = Ptlist.getcount (); bRet = Ptlist.isempty ();  //position PS = 10; CPoint point = Ptlist.getat (Ptlist.findindex);p tlist.removeat (Ptlist.findindex (10)); The deletion of the specified element ncount = Ptlist.getcount ();p oint = Ptlist.getat (ptlist.findindex);  //List Traversal position pos= Ptlist.getheadposition (); for (idx = 0; idx < Ptlist.getcount (); ++idx) { cpoint pt = ptlist.getnext (POS);  p T.x = 123;}  point.x = Point.y = 999;ptlist.setat (Ptlist.findindex (5), point);  pos = Ptlist.getheadposition (); for (idx = 0 ; IDX < Ptlist.getcount (); ++IDX) { cpoint pt = ptlist.getnext (pos);  pt = pt;}  ptlist.removeall (); ncount = Ptlist.getcount (); bRet = Ptlist.isempty ();  ⑩, ※※※ the difference between a list and an array ※※※ array is the element that is stored in memory continuously, Because each element occupies the same memory, you can quickly access any element in the array by subscript. But if you want to add an element to an array, you need to move a large number of elements, empty the space of an element in memory, and then put the added elements in it. Similarly, if you want to delete an element, you also need to move a large number of elements to fill out the moved element. The   list is the opposite, and the elements in the list are not stored sequentially in memory, but are linked by pointers in the elements that exist. For example: The previous element has a pointer to the next element, and so on, until the last element. If you want to access an element in a linked list, you need to start with the first element and always find the desired element position. But adding and removing an element is very simple for a linked list data structure, as long as you modify the pointer in the element.  ※※※ from the above comparison can be seen, if you need to quickly access the data, little or no insertion and deletion of elements, you should use the array, on the contrary, if you need to insert and delete elements often need to use the list!  ====================================================@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@============== ======================================&NBSP;11, the basic description of the mapping table: the Mapping table class is also calledAs a "dictionary", just like a two-column table, a column is a keyword, a column is a data item, they are one by one corresponding, is in the form of key-value pairs appear! Keyword is unique (this is important), given a keyword, the mapping table will quickly find the corresponding data item. The lookup of the mapping table is done in a hash table, so finding numeric entries in the mapping table is fast. The mapping class is best suited for situations where you need to quickly retrieve keywords.   For example: All employees of the company have a work number and their name, work number is the keyword, give a work number, you can quickly find the corresponding name! Name can be repeated, or have the same name, but the work number must not appear duplicate! &NBSP;12, the dynamic mapping table Class in MFC is:cmap  Although CMAP is a template class, Microsoft has defined in MFC for a variety of commonly used variable types: cmapwordtoptr, CMapPtrToWord, CMapPtrToPtr, CMapWordToOb, CMapStringToPtr, CMapStringToOb, cmapstringtostring, etc., so, if you use the data structure in the program has the appropriate types of the above, Can be used directly, no need to repeat the definition, see msdn! &NBSP;13, CMap type Object definition: Where the arg_ prefix is the incoming type, the other is the return type CMap <int, int, CString, lpctstr> intmapstring; CMap <int, int, int, int> intmapint; CMap <int, int, CPoint, CPoint &> intmappoint; CMap <long, Long, CButton, CButton &> Longmapbutton; CMap <cstring, LPCTSTR, int, int> strmapstring;//when using the basic type, the second to fourth parameter can be used without reference, but the second to fourth parameter is best to pass the object's reference when the class type conforms to the type, and/or This avoids the call of the copy constructor and improves efficiency! &NBSP;14, CMap class use: cmap<int, int, CString, lpctstr> intmapstring (a);  cstring strtext;for (int idx = 0; IDX &lt ; 20; ++IDX) { strtext.format (_t ("%s%d"), _t ("The number is:"), IDX);  intmapstring.setat (idx, strText);}  int ncount = Intmapstring.getcount (); BOOL BRet = Intmapstring.isempty ();  strtext.empty (); Intmapstring.lookup (strText);// Find the element Intmapstring.removekey (10) according to Key; Delete the element according to key ncount = Intmapstring.getcount ();  strtext.empty (); Intmapstring.lookup (StrText); Can you find it? Traversal of the  //mapping table int nkey = 0; POSITION pos = Intmapstring.getstartposition (); while (pos! = NULL) { strtext.empty ();  INTMAPSTRING.GETNEXTASSOC (POS, Nkey, StrText);}  intmapstring.setat (5, _t ("abcdefg123"));  intmapstring.removeall (); ncount = Intmapstring.getcount (); BRet = Intmapstring.isempty ();   ★ after-school assignment: 1, practice using the use of CStringArray and other classes;-------------------------------------End -------------------------------------------

VC + + Basic class [chart]---Dynamic array and dynamic link list

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.