c ++ map

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • swathikah
    New Member
    • Oct 2008
    • 2

    #1

    c ++ map

    hey i wanna sort ma map using the second value which is an int.help me do this

    Map:
    [code=cpp]
    class MyMap
    {
    public:
    typedef string KeyType;
    typedef int ValueType;
    typedef pair<KeyType, ValueType> ItemType;

    private:
    typedef map<KeyType, ValueType> MapType;
    MapType map_;

    public:

    bool AddItem( KeyType const & k, ValueType const & v )
    {
    // insert returns a std::pair of the position
    // and whether the insertion succeeded.
    // We are only interested in the success value.
    return map_.insert( make_pair(k,v) ).second;
    }
    bool comp_int(const int &s1, const int &s2)
    {
    return(s1>s2 ? true:false);
    }
    bool Find( KeyType const & k, ValueType & v )
    {
    //sort(map_.begin (),map_.end());//needs to be sorted before the search happens.sorted in ascending order.the int value(i.e the v should be used for sorting)
    MapType::iterat or pos( map_.find(k) );
    if ( pos != map_.end() )
    {
    v = pos->second;
    return true;
    }
    else
    {
    return false;
    }
    }
    };
    [/code]
    Main:
    [code=cpp]
    int main()
    {
    MyMap p1;

    for(int i=0;i<5;i++)
    {
    cout << "enter mac_addr value pair: ";
    MyMap::KeyType key;
    MyMap::ValueTyp e value;
    cin >> key;
    cin >> value;
    p1.AddItem(key, value) ;
    //{
    // cerr << "Mac address duplicate, data not stored." << endl;
    //}
    }

    cout << "Enter mac address to lookup: ";
    MyMap::KeyType searchKey;
    cin >> searchKey;
    MyMap::ValueTyp e foundValue;
    if ( p1.Find( searchKey, foundValue) )
    {
    cout << "Found value " << foundValue << " for search key " << searchKey << endl;
    }
    else
    {
    p1.AddItem(sear chKey,0);
    cout << "No such mac address as " << searchKey << " in map." << endl;
    }
    return 0;
    }
    [/code]
    //in this u have to sort the map inoder to compare properly.coz in this what happpens is when u insert the lastest ack,it is stored behind coz first in first out concept.so when it searches it encounters the first inserted sender and ack which mite be incorrect.
    //so c if u can sort the map
  • gpraghuram
    Recognized Expert Top Contributor
    • Mar 2007
    • 1275

    #2
    Whe you define a map you can use the 3rd paramater to define your own comparison function.
    Try to use that.

    raghu

    Comment

    • swathikah
      New Member
      • Oct 2008
      • 2

      #3
      i didnt understand what u said.can u redefine the entire code and post it.thank u

      Comment

      • gpraghuram
        Recognized Expert Top Contributor
        • Mar 2007
        • 1275

        #4
        Originally posted by swathikah
        i didnt understand what u said.can u redefine the entire code and post it.thank u

        If you want to sort the map in a different way using index my previous idea would work.
        Since you want to sort by value then you should use a different map making the value as the index.


        raghu

        Comment

        • sicarie
          Recognized Expert Specialist
          • Nov 2006
          • 4677

          #5
          Originally posted by swathikah
          i didnt understand what u said.can u redefine the entire code and post it.thank u
          Per the Posting Guidelines of this site, nobody is going to do your work for you. Please make an attempt at the suggestion, or ask a more specific question about what you do not understand.

          Comment

          Working...