Question on Maps

This is a discussion on Question on Maps within the C++ Programming forums, part of the General Programming Boards category; consider an example where i have a map holding keys :1000,2000,10000,30000 mapped values are 1,2,3,4 ie: 1--- 10000 3-----10000 2----20000 ...

  1. #1
    dpp
    dpp is offline
    Registered User
    Join Date
    Jan 2009
    Posts
    197

    Question on Maps

    consider an example where i have a map holding keys :1000,2000,10000,30000
    mapped values are 1,2,3,4
    ie:

    1--- 10000
    3-----10000
    2----20000
    4----30000


    all i do to delete an elemnt is withy key 10000:
    Code:
    it= mymap.find(10000);
                                                    mymap.erase(it);
    But wat if i want the 3-10000 pair to be deleted..
    but not 1-10000
    can ialso specify the mapped value to delete the pair ...
    If yes what snippet should i add to the above

  2. #2
    The larch
    Join Date
    May 2006
    Posts
    3,573
    Firstly, are you using a std::multi_map, because std::map can only contain one item with key 10000.

    Code:
         std::map<int, int> map;
         map[10000] = 1;
         map[10000] = 3; //overwrites the mapped value (1) with 3
    Then you'll probably need to find the range with key value 10000 (equal_range) and loop over those, erasing the items that have a desired mapped_value (3), leaving the others (1).
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  3. #3
    dpp
    dpp is offline
    Registered User
    Join Date
    Jan 2009
    Posts
    197
    s i use Multimaps...fine thanks for the reply

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Alice....
    By Lurker in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 06-20-2005, 02:51 PM
  2. Debugging question
    By o_0 in forum C Programming
    Replies: 9
    Last Post: 10-10-2004, 05:51 PM
  3. Question about pointers #2
    By maxhavoc in forum C++ Programming
    Replies: 28
    Last Post: 06-21-2004, 12:52 PM
  4. Question...
    By TechWins in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 07-28-2003, 09:47 PM
  5. Question, question!
    By oskilian in forum A Brief History of Cprogramming.com
    Replies: 5
    Last Post: 12-24-2001, 12:47 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21