Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

CMapStringToOb

Status
Not open for further replies.

usernew

Programmer
Feb 6, 2004
1
US
CMapStringToOb Mapping doesn't allow us to retrieve the objects in the order we added to the map...that is the sequence is not stored and it retrieves them randomly. True or false?

If true, any better suggestion as to what map can do it sequentially?
 
True

The [tt]CMapStringTo...[/tt] classes don't guarantee any order for the items, they are just "bags" (as the term used to be) into which you can dump stuff with a name and get them out with a name. Inside the bag, they're all jumbled up.

If you want a sequential list... ...not sure if there's a FIFO/LIFO map class in MFC, never had necessity to use one...

[tt]_______________________________________
Roger [pc2]
The Eileens are out there[/tt]
 
A solution is to put the sequence ( the order in which you add the objects to the map) in the key and the name of the key in the object associated with that key.
Something like :
CMyObjClass : public Cobject
{
public:
CMyObjClass( CString & s)
{
ObjName = s;
}
CString ObjName;
// other data

}
CMapStringToOb map;

CString key="1";
CMyObjClass o1 = new CMyObjClass("george");
map.Add(key,o1);
key = "2";
CMyObjClass o2 = new CMyObjClass("john");
map.Add(key,o2);

Lookup the map in the order of the sequence:

CMyObjClass *rObj=NULL;
CString sKey;
for (int i=0;i<map.GetCount();i++)
{
sKey.Format(&quot;%d&quot;,i);
Lookup(sKey, ( CObject*& ) &rObj);
if (rObj != NULL)
{
// process rObj->ObjName object with the &quot;i&quot; sequence
}
}
-obislavu-







 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top