Set question

This is a discussion on Set question within the C++ Programming forums, part of the General Programming Boards category; Let me model my requirement with a simple example... I use multiset as a part of my program... say for ...

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

    Set question

    Let me model my requirement with a simple example...

    I use multiset as a part of my program...
    say for eg: 500,400,300,200,100
    are the value to be inserted in the set

    All i have to do is assoaciate each elemnt above with some specified number..

    say 500 assoaciated with 1
    sameways:
    400-2
    300-3
    200-4
    100-5

    whenevere the elemnts are pushed and finally extracted(ofcourse minelement is extrcted if minheap)

    say 100 is extracted...Now i should be ina postion to get its associated index ie '5' straight away...

    how do i map them...???
    plz help me to get it solved

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    How do you map them? Why not use a (multi)map?

  3. #3
    dpp
    dpp is offline
    Registered User
    Join Date
    Jan 2009
    Posts
    197
    can u plz elabrate on it...plz
    should i use map with set
    wats the syntax... how does it meet my requirement

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Quote Originally Posted by dpp View Post
    can u plz elabrate on it...plz
    should i use map with set
    wats the syntax... how does it meet my requirement
    A map is your requirement -- a bunch of pairs of values, each a data value associated with a key value, stored in sorted order by keys.

    Whatever resource you learned sets from, flip forward three pages or so and you'll find map.

  5. #5
    dpp
    dpp is offline
    Registered User
    Join Date
    Jan 2009
    Posts
    197
    but will map be in sorted order

  6. #6
    dpp
    dpp is offline
    Registered User
    Join Date
    Jan 2009
    Posts
    197
    Plz let me know is there a function for sort in map....

    It is not contiguous as far as i know

  7. #7
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    As I mentioned before, elements in a map are stored in sorted order by keys. (You can specify your own ordering function if you want.)

    If you have lost your book between hearing about sets and trying to do this now, you can always find things on the web.

  8. #8
    dpp
    dpp is offline
    Registered User
    Join Date
    Jan 2009
    Posts
    197
    erasing doubt ;(

    Code:
     mymap.insert ( pair<long long int ,long long int>(200,1) );
               mymap.insert ( pair<long long int ,long long int>(201,2) );
                mymap.insert ( pair<long long int ,long long int>(201,3) );
                it=mymap.find(1);
                   mymap.erase (it);
    all i wanted is to erase the elemnt with the mapped value 1...
    but this doesnt work

    but i am able to do mympa.erase(201) (delete by key which i dont need)


    I want it to erase based on the mappd value

  9. #9
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    1) What if there's more than one element with that value?
    2) What if there's no element with that value?
    3) To find an element with a given value, you need to use find_if.

    (I suppose I should add that find_if is not a member of multimap, but a generic <algorithm>.)

  10. #10
    dpp
    dpp is offline
    Registered User
    Join Date
    Jan 2009
    Posts
    197
    But for the example i mentioned above

    1 was an element
    but still it dint work

    why???

  11. #11
    dpp
    dpp is offline
    Registered User
    Join Date
    Jan 2009
    Posts
    197
    when i did
    Code:
    it=mymap.find(1);
                   mymap.erase (it);
    this, The corresponding elemnt was not erased....
    Though '1' is a part of the elements

  12. #12
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Quote Originally Posted by dpp View Post
    But for the example i mentioned above

    1 was an element
    but still it dint work

    why???
    1 is most definitely not an element. 200 is, and 201, and another 201. Read the last answer again.

  13. #13
    dpp
    dpp is offline
    Registered User
    Join Date
    Jan 2009
    Posts
    197
    oops sorry...

    if i do
    Code:
    mymap1.erase(201)
    if 201 has more than one elements being mapped (but i want only one of its mapped value to be removed)
    Is it possible to do that
    Code:
    mymap.insert ( pair<long long int ,long long int>(200,1) );
               mymap.insert ( pair<long long int ,long long int>(201,2) );
                mymap.insert ( pair<long long int ,long long int>(201,3) );
    say i want only 201,2 to be removed but not 201,3...

    Thanks for ur patience

  14. #14
    dpp
    dpp is offline
    Registered User
    Join Date
    Jan 2009
    Posts
    197
    i found it thanks

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. bit operation question
    By mr_coffee in forum C Programming
    Replies: 3
    Last Post: 04-07-2009, 05:00 PM
  2. Replies: 8
    Last Post: 01-18-2008, 03:06 AM
  3. The new FAQ
    By Hammer in forum A Brief History of Cprogramming.com
    Replies: 34
    Last Post: 08-30-2006, 10:05 AM
  4. OpenGL Window
    By Morgul in forum Game Programming
    Replies: 1
    Last Post: 05-15-2005, 12:34 PM

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