Click here to Skip to main content
15,896,111 members
Articles / Desktop Programming / MFC

A Presentation of the STL Vector Container

Rate me:
Please Sign up or sign in to vote.
4.83/5 (53 votes)
3 Nov 20039 min read 309.1K   2.2K   60  
Presenting the std::vector with a discussion on STL algorithms and predicates.
// STL_with_MFCDlg.h : header file
//

#if !defined(AFX_STL_WITH_MFCDLG_H__506BF7FC_CB1D_45DB_8396_0326B89F3CB4__INCLUDED_)
#define AFX_STL_WITH_MFCDLG_H__506BF7FC_CB1D_45DB_8396_0326B89F3CB4__INCLUDED_

#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000

#include <algorithm>
#include <functional>
#include <vector>
#include "DemoDlg.h"

// a function object to delete a pointer
// from a for_each algorithm
struct DeletePtr
{
	template<typename T>
		void operator()(const T* ptr) const { delete ptr; }
};

// a function object to close a CWnd
// from a for_each algorithm
struct CloseWnd
{
	void operator()(CWnd* wnd) const { wnd->PostMessage(WM_CLOSE); }
};

// an enum for modes
enum findmodes 
{	
	FM_INVALID = 0,
	FM_IS,
	FM_STARTSWITH,
	FM_ENDSWITH,
	FM_CONTAINS		
};

// a typedef to pass to our predicate
typedef struct tagFindDlg
{
	UINT iMode;
	CString szWindowTitle;
} FindDlg;
typedef FindDlg* LPFIND_DIALOG;

// declare a function object to use as a 
// predicate for remove_if().  This predicate
// will close and delete a CDialog* if window
// titles match by the specified mode.
class CloseAndDeleteByTitle :
	public std::unary_function<CDialog, bool>
{	
public:
	CloseAndDeleteByTitle(const LPFIND_DIALOG lpFDD) : m_lpFDD(lpFDD) {}
	
	bool operator()(CDialog* w) const
	{
		bool retVal = false;
		CString szWndTitle = "";
		w->GetWindowText(szWndTitle);
		
		switch(m_lpFDD->iMode)
		{
		case FM_IS:
			{
				retVal = (szWndTitle == m_lpFDD->szWindowTitle);
				break;
			}
		case FM_STARTSWITH:
			{
				retVal = (szWndTitle.Left(m_lpFDD->szWindowTitle.GetLength()) ==
					m_lpFDD->szWindowTitle);
				break;
			}
		case FM_ENDSWITH:
			{
				retVal = (szWndTitle.Right(m_lpFDD->szWindowTitle.GetLength()) ==
					m_lpFDD->szWindowTitle);
				break;
			}
		case FM_CONTAINS:
			{
				retVal = (szWndTitle.Find(m_lpFDD->szWindowTitle) != -1);
				break;
			}
		}
		
		if(retVal)
		{
			w->PostMessage(WM_CLOSE);
			delete w;
		}
	
		return retVal;
	}
	
private: 
	LPFIND_DIALOG m_lpFDD;
};

/////////////////////////////////////////////////////////////////////////////
// CSTL_with_MFCDlg dialog

class CSTL_with_MFCDlg : public CDialog
{
// Construction
public:
	CSTL_with_MFCDlg(CWnd* pParent = NULL);	// standard constructor

// Dialog Data
	//{{AFX_DATA(CSTL_with_MFCDlg)
	enum { IDD = IDD_STL_WITH_MFC_DIALOG };
		// NOTE: the ClassWizard will add data members here
	//}}AFX_DATA

	// ClassWizard generated virtual function overrides
	//{{AFX_VIRTUAL(CSTL_with_MFCDlg)
	protected:
	virtual void DoDataExchange(CDataExchange* pDX);	// DDX/DDV support
	//}}AFX_VIRTUAL

// Implementation
protected:
	std::vector<CDemoDlg*> m_vpDemoDialogs;
	HICON m_hIcon;

	// Generated message map functions
	//{{AFX_MSG(CSTL_with_MFCDlg)
	virtual BOOL OnInitDialog();
	afx_msg void OnSysCommand(UINT nID, LPARAM lParam);
	afx_msg void OnPaint();
	afx_msg HCURSOR OnQueryDragIcon();
	afx_msg void OnCreateDemoDlg();
	afx_msg void OnRemoveAllDemoDialogs();
	afx_msg void OnClose();
	afx_msg void OnRemoveDemoDlg();
	//}}AFX_MSG
	DECLARE_MESSAGE_MAP()
};

//{{AFX_INSERT_LOCATION}}
// Microsoft Visual C++ will insert additional declarations immediately before the previous line.

#endif // !defined(AFX_STL_WITH_MFCDLG_H__506BF7FC_CB1D_45DB_8396_0326B89F3CB4__INCLUDED_)

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
CEO Aspen Insights
United States United States
Walter Storm is currently doing quantitative research and data science. Originally from Tunkhannock, PA., he has a B.S. in Aerospace Engineering from Embry-Riddle Aeronautical University[^], and an M.S. in Systems Engineering from SMU[^]. He has been professionally developing software in some form or another since January of 2001.

View Walter Storm's profile on LinkedIn.[^]

Comments and Discussions