Dela via


COLLECT Sample: Illustrates MFC Collection Classes

The COLLECT sample illustrates a wide variety of collection classes offered by the Microsoft Foundation Class Library (MFC).

Security noteSecurity Note:

This sample code is provided to illustrate a concept and should not be used in applications or Web sites, as it may not illustrate the safest coding practices. Microsoft assumes no liability for incidental or consequential damages should the sample code be used for purposes other than as intended.

To get samples and instructions for installing them:

  • On the Visual Studio Help menu, click Samples.

    For more information, see Visual Studio Samples.

  • The most recent version and complete list of samples is available online from the Visual Studio 2008 Samples page.

  • You can also locate samples on your computer's hard disk. By default, samples and a Readme file are copied into a folder under \Program Files\Visual Studio 9.0\Samples\. For Express editions of Visual Studio, all samples are located online.

Building and Running the Sample

To build and run the COLLECT sample

  1. Open the solution collect.sln.

  2. On the Build menu, click Build.

  3. On the Debug menu, click Start Without Debugging.

The Example menu lists nine collection class illustrations organized according to the following table. Each example presents a form view that exercises many common collection operations:

  • Adding a new element.

  • Inserting a new element in the middle of a list.

  • Searching an element (by clicking its entry in the dialog box's list box).

  • Searching an entry in the map by entering the Key and clicking Find.

  • Updating the value of an element.

  • Removing an element.

  • Removing all the elements of the collection.

You can save changes to all the examples by clicking Save on the File menu and read them back by clicking Open on the File menu.

MFC Collection Classes

The MFC collection classes can be classified according to shape and design. MFC offers classes for three types of collection shapes:

  • Lists

  • Arrays

  • Maps

MFC also offers three types of designs:

  • No use of C++ templates.

  • Use of a C++ template–based collection of simple elements.

  • Use of a C++ template–based collection of type-safe pointers.

The following table identifies all the MFC collection classes sorted according to shape and design. The COLLECT sample illustrates nine of these collections, one from each cell in the table. Each represents a distinct combination of shape and design. Also, because usage of the CTypedPtrArray and CTypedPtrList template-based collections differs according to whether they are used to hold pointers to CObjects or to non-CObjects, the COLLECT sample illustrates both cases. The nine collections illustrated by COLLECT are indicated with an asterisk.

Collection shapes

Non-template based

Template-based simple elements

Template-based typeSafe pointers

List

CStringList*

CList*

CTypedPtrList

 

CObList

 

(of pointers)*

 

CPtrList

 

CTypedPtrList

 

 

 

(of CObjects)

Array

CByteArray

CArray*

CTypedPtrArray

 

CUIntArray

 

(of pointers)

 

CWordArray*

 

 

 

CDWordArray

 

CTypedPtrArray

 

CStringArray

 

(of CObjects)*

 

CPtrArray

 

 

 

CObArray

 

 

Map

CMapWordToPtr

CMap*

CTypedPtrMap*

 

CMapWordToOb

 

 

 

CMapStringToString*

 

 

 

CMapStringToPtr

 

 

 

CMapStringToOb

 

 

 

CMapPtrToWord

 

 

 

CMapPtrToPtr

 

 

Additional COLLECT Features

In addition to illustrating the MFC collection classes, COLLECT also illustrates:

  • Coordinating the addition and removal of entries in a list box with the addition and removal of entries in a collection.

  • Switching between different views of a document, with only one shown at a time. That is, although COLLECT has many different views, it has a single document interface (SDI) instead of a multiple document interface (MDI).

Keywords

This sample demonstrates the following keywords:

AfxMessageBox; CCmdUI::SetCheck; CControlBar::EnableDocking; CDialog::DoModal; CDocument::DeleteContents; CDocument::OnNewDocument; CFrameWnd::DockControlBar; CFrameWnd::EnableDocking; CFrameWnd::GetActiveDocument; CFrameWnd::GetActiveView; CFrameWnd::RecalcLayout; CFrameWnd::SetActiveView; CListBox::AddString; CListBox::DeleteString; CListBox::FindString; CListBox::GetCurSel; CListBox::GetItemData; CListBox::GetItemDataPtr; CListBox::GetText; CListBox::InsertString; CListBox::ResetContent; CListBox::SetItemData; CListBox::SetItemDataPtr; CObject::AssertValid; CObject::Dump; CObject::Serialize; CStatusBar::Create; CStatusBar::SetIndicators; CString::Empty; CString::Format; CString::LoadString; CToolBar::Create; CToolBar::LoadBitmap; CToolBar::SetButtons; CView::DoPreparePrinting; CView::GetDocument; CView::OnBeginPrinting; CView::OnDraw; CView::OnEndPrinting; CView::OnInitialUpdate; CView::OnPreparePrinting; CView::OnUpdate; CWinApp::AddDocTemplate; CWinApp::InitInstance; CWinApp::LoadStdProfileSettings; CWinApp::OnFileNew; CWnd::DestroyWindow; CWnd::DoDataExchange; CWnd::OnCreate; CWnd::ShowWindow; CWnd::UpdateData; LoadString; SetWindowLong

Note

Some samples, such as this one, have not been modified to reflect the changes in the Visual C++ wizards, libraries, and compiler, but still demonstrate how to complete your desired task.

See Also

Other Resources

MFC Samples