String-> CMap Structure

My requirement is that, given the string as the key to the card, I should be able to get the structure.

Can anyone post an example code for this?

Example:

struct
{
int a;
int b;
int c;
}struct_sample;

string1 → strcut_sample

+5
source share
2 answers
CMap<CString,LPCTSTR, struct_sample,struct_sample> myMap;

struct_sample aTest;
aTest.a = 1;
aTest.b = 2;
aTest.c = 3;
myMap.SetAt("test",aTest);
...

    struct_sample aLookupTest;
    BOOL bExists = myMap.Lookup("test",aLookupTest); //Retrieves the 
                             //struct_sample corresponding to "test".

Example from MDSN for more information on CMap.

+6
source

If you are ready to stick with MFC, follow aJ's answer.

Otherwise, you're better off with a standard library card. Be sure to check the documentation - there is much to learn. I usually use http://www.sgi.com/tech/stl/table_of_contents.html .

#include <map>
#include <string>
#include <utility> // for make_pair

using namespace std;

int main() {
    std::map< string, struct_sample > myMap;
    const struct_sample aTest = { 1,2,3 };
    myMap.insert(make_pair( "test", aTest ) );
    myMap[ "test2" ] = aTest; // does a default construction of the mapped struct, first => little slower than the map::insert

    const map<string, struct_sample >::const_iterator aLookupTest = myMap.find( "test" );
    const bool bExists = aLookupTest != myMap.end();

    aLookupTest->second.a = 10;
    myMap[ "test" ].b = 20;

}

: typedef .

+2

All Articles