Dave's Brain

Browse - programming tips - mfc interate through cmap

Date: 2008jan21
Platform: MFC
Language: C/C++

Q.  How do I interate through an MFC CMap<> ?

A.

// Assuming you have a string -> pointer map like this
typedef	CMap< CString, LPCSTR, Node*, Node*> CMapStringToNode;

// Loop through all elements this way:
void interate_thru_cmap(CMapStringToNode &map)
{
	CString		strJunk;
	Node		*node;

	pos = map.GetStartPosition();
	for (;;)
	{
		if (pos == NULL) break;
		map.GetNextAssoc(pos, strJunk, node);

		// Do something with node ...
	}
}

// STL's map<> is easier to work with.
What this info useful to you? You can donate to say thanks

Add a comment

Sign in to add a comment
Copyright © 2008-2012, dave - Code samples on Dave's Brain is licensed under the Creative Commons Attribution 2.5 License. However other material, including English text has all rights reserved.
Advertisements: