VC entry book (9)

zhaozj2021-02-16  53

Collection class experience

He Zhidan

MFC provides collection class (Collect) dedicated to data object storage and management, MFC's collection class is divided into three categories, which are used to handle data structure of three types of different nature: Table (List, a double-linked table similar to data structure), Array and Mapping (MAP, features similar to dictionary).

First, array use experience

Prototype: Template Class Carray: Public COBJECT

Simply say that when you entered, use the arg_type class, when the output is used, automatically realizes the conversion.

Specific implementation can be seen in C: / Program Files / Microsoft Visual Studio / VC98 / MFC / include / AFXTEMPL.H.

Such as:

#include "afxtempl.h" // That class requires those headers to view MSDN, in summary

Carray vars;

Vars.setsize (3, 1);

Vars.Setat (0, 'a');

INT x = vars.getat (0);

This nature is more effective for custom classes, because I can control the transformation process by overloading "=".

Set breakpoints, we will find that there are three class members: m_nsize, m_nmaxsize, m_ngrowby, respectively correspond to the number of spaces, and how many elements of each of the elements are opened when the space is opened.

Common functions:

INT getsize () const Gets the value of m_nsize.

INT getupperbound () const; an array of upper bounds, m_nsize-1.

Void setsize (int nnewsize, int ngrowby = -1); three class members will change, and the previous content is not necessarily lost.

Void freeextra (); sorted unnecessary space to make m_nmaxsize = m_nsize.

Void RemoveAll (); Delete all elements.

TYPE GETAT (INT NINDEX) Const; gets the value of the NOINDEX (starting from 0) element.

Void sett (int NINDEX, Arg_Type NewElement); NINDEX cannot be off.

Const type * getdata () const; returns the data pointer.

Void SetaTtive (int NINDEX, ARG_TYPE NEWELEMENT); and SETAT is similar, if too large, will open a new space.

INT Add (arg_type newelement); add an element, M_NSize plus one.

INT APPEND (Const Carray & SRC); / / Plus a group of types of arrays.

Void insertat (int NINDEX, Arg_Type NewElement, int ncount = 1); void insertat (int NStartIndex, Carray * PNewARRAY);

TYPE & OPERATOR [] (INT NINDEX); an array of C-style operates.

TYPE OPERATOR [] (INT NINDEX) Const;

Other arrays are:

CbyteArray supports an array of bytes.

CWORDARRAY supports the word array.

CDWORDARRAY supports double-byte array.

Cobarray supports the COJECT type pointer array.

CPTRARRAY supports the VOID type pointer array.

CuintArray supports an unsigned array array.

CStringArray supports a CString array.

Usage is basically consistent with the above.

Second, the table use experience

I take CStringList as an example, talk about the experience of using the table. Note: View msdn Vc_Assist6. The following example uses almost all functions of this class .cstringList var (15);

Position position;

Position = var.insertafter (NULL, "ITEM1");

Position = var.insertafter (position, "item3");

Position = var.insertbefore (position, "item2");

For (position = var.getheadPosition (); null! = position;)

AfxMessageBox (Var.getNext (position));

For (position = var.gettailposition (); null! = position; var.getprev (position))

{

CString Str;

Str = var.getat (position);

IF ("item3" == STR)

{

Var.removeat (position);

}

Else

{

Str.makeupper ();

Var.Setat (position, str);

}

}

Var.removehead ();

Var.RemoveTail ();

Var.removeall ();

Var.Addhead ("HE");

Var.Addtail ("DAN");

Position = var.find ("HE");

Var.Setat (Position, "HE");

Position = var.findIndex (1);

Var.set (Position, "Dan");

// end

Below is an introduction to such a function.

Position insertbefore (position position, cobject * newelement);

Position insertafter (position position, cobject * newelement);

Insert a new element before or after a position.

Position GetPosition () Const;

Position gettailposition () const;

Get your head position and tail position.

COBJECT * & GetNext (position & rposition);

COBJECT * GetNext (position & rposition) const;

COBJECT * & GETPREV (Position & rposition);

COBJECT * GETPREV (POSITION & RPSITION) Const;

Get the latter element or the previous element, pay attention to RPSITION.

COBJECT * & GETAT (POSITION POSITION);

COBJECT * GETAT (POSITION POSITION) Const;

The element is obtained according to the location.

Void Setat (Position Pos, CObject * newElement);

Set the element according to the location.

Void Removeat (Position Position);

Delete the element according to the location.

COBJECT * REMOVEHEAD (); Delete and return to head elements

COBJECT * REMOVETAIL (); Delete and return tail elements

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

New Post(0)