A Safer STL Container Class
Posted
by Alex Kravchenko
on November 1st, 2000
return (*((unsigned int*)((int)pBlock + 4)) == 0xfeedface);I also added some functions to make it MFC like: GetFirst(), GetNext(), RemoveLastRead(), etc. In the sample code I added 10 elements into the map. Then I deleted elements 5 and 7 outside the map. Then I looped through displaying the content. Notice that after going through all the elements CTidyMap reports 8 to be the actual size.
Example Usage
CTidyMap<CElement, int> mymap;
CElement* pElement[10];
for(int i = 0; i < 10; i++) {
pElement[i] = new CElement(i);
mymap.Add(pElement[i], i);
}
cout << "Number of elements in the map: "
<< mymap.GetCount() << endl;
delete pElement[5];
cout << "Deleted Element number 5" << endl;
delete pElement[7];
cout << "Deleted Element number 7" << endl;
cout << "Still does not know about elements being deleted, size: "
<< mymap.GetCount() << endl;
CElement* pExElem = mymap.GetFirst();
while(pExElem) {
cout << "Found Element number: "
<< pExElem->GetIndex()
<< endl;
pExElem = mymap.GetNext();
}
cout << "Now the number of elements in the map: "
<< mymap.GetCount()
<< endl;
Output
Number of elements in the map: 10 Deleted Element number 5 Deleted Element number 7 Still does not know about elements being deleted, size: 10 Found Element number: 0 Found Element number: 1 Found Element number: 2 Found Element number: 3 Found Element number: 4 Found Element number: 6 Found Element number: 8 Found Element number: 9 Now the number of elements in the map: 8
Bonus
I also made the feedface optional so if you initialize it like this:CTidyMap<CElement, int> mymap = false;you can watch the program crash.

Comments
There are no comments yet. Be the first to comment!