How to perform Map Implemantation in C++

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Girish Kanakagiri
    New Member
    • May 2007
    • 93

    How to perform Map Implemantation in C++

    Say for example I want to store the name of object and corresponding value
    for the same; all in the form of strings.

    How can I implement it, so that I would be able to retrieve the value once
    I find the Object name.

    Please Can anyone explain me with an Example ?

    Thanks in advance.
  • gpraghuram
    Recognized Expert Top Contributor
    • Mar 2007
    • 1275

    #2
    HI,
    I presume that u are asking for example with STL map.
    Code:
    map<string,string> smap;
    string s1("index");
    string s2("value");
    smap[s1]=s2;
    If u need some thing else then explain more on that..
    Thanks
    Raghuram

    Comment

    • Girish Kanakagiri
      New Member
      • May 2007
      • 93

      #3
      Originally posted by gpraghuram
      HI,
      I presume that u are asking for example with STL map.
      Code:
      map<string,string> smap;
      string s1("index");
      string s2("value");
      smap[s1]=s2;
      If u need some thing else then explain more on that..
      Thanks
      Raghuram
      Thanks for your response, Your right I am asking for STL map.
      The scenario is like this ...
      I nead map < objname , map1 > // here map1 is value for objname
      map1 < propertyname , value > // actual value comes here.

      What exactly I mean is -> I should have a name value pair, in this the value
      should again be a map with name value pair and here I will find the actual value

      Thanks in advance,
      Girish.

      Comment

      • weaknessforcats
        Recognized Expert Expert
        • Mar 2007
        • 9214

        #4
        All map elements must be pairs.

        [code=cpp]
        map<string,stri ng> smap;
        pair<string, string> p;
        p.first = "index";
        p.second = "value";
        smap.insert(p);
        //
        //To retreive:
        //
        //
        map<string, string>::iterat or answer;
        //
        answer = smap.find("inde x");

        cout << (*answer).first << endl; //you see value
        [/code]

        Comment

        Working...