Sorting a map

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • ycinar
    New Member
    • Oct 2007
    • 39

    Sorting a map

    i have 2 maps like below:

    Code:
    typedef std::map<std::string, ObjA> MapA;
    typedef std::map<ObjA, std::string> MapB;
    
    MapA mapa;
    MapB mapb;
    they are actually pretty same maps, just key and value are swaped.

    i want to find out whether or not a value (which is string) is in the mapB like this
    Code:
    ObjA function (const char * value){
    for (MapB::const_iterator it = _mapb.begin(); it != _mapb.end(); ++it){
     // how do i fill here.. 
     // mapb is something like this:
     // mapb <ObjA, value>
     // like how to check if it == value and return ObjA
    }
    }
    Thanks in advance
  • gpraghuram
    Recognized Expert Top Contributor
    • Mar 2007
    • 1275

    #2
    Originally posted by ycinar
    i have 2 maps like below:

    Code:
    typedef std::map<std::string, ObjA> MapA;
    typedef std::map<ObjA, std::string> MapB;
    
    MapA mapa;
    MapB mapb;
    they are actually pretty same maps, just key and value are swaped.

    i want to find out whether or not a value (which is string) is in the mapB like this
    Code:
    ObjA function (const char * value){
    for (MapB::const_iterator it = _mapb.begin(); it != _mapb.end(); ++it){
     // how do i fill here.. 
     // mapb is something like this:
     // mapb <ObjA, value>
     // like how to check if it == value and return ObjA
    }
    }
    Thanks in advance
    Check this code

    [code=cpp]
    ObjA check(const char * value)
    {
    for (MapB::const_it erator it = mapb.begin(); it != mapb.end(); ++it)
    {
    if (strcmp((*it).s econd.c_str(),v alue) == 0)
    {
    return (*it).first;
    }
    }
    }
    [/code]

    Raghuram

    Comment

    • weaknessforcats
      Recognized Expert Expert
      • Mar 2007
      • 9214

      #3
      strcmp is deprecated in C++. Use the string type.

      [code=cpp]
      ObjA check(const string& value)
      {
      for (MapB::const_it erator it = mapb.begin(); it != mapb.end(); ++it)
      {
      if ((*it).second == value)
      {
      return (*it).first;
      }
      }
      }
      [/code]

      There should be no C strings in a C++ program.

      Comment

      Working...