CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6
  1. #1
    Join Date
    Dec 2000
    Posts
    129

    std::map position

    I have a std::map that is full of string, string elements that are presorted in a particular way. I need to know what position two elements are in, so I can know if one element is more important than the other. For example:

    1: "10","Test"
    2: "13","Test2"
    3: "4","Test3"
    4: "66","Test4"
    5: "34","Test5"

    Someone enters "66", "13", "34" I need to know that the order should go "34", "66", "13".

  2. #2
    Join Date
    Apr 1999
    Location
    Altrincham, England
    Posts
    4,470
    I'm not quite sure what you're asking for here, but std::map holds its element sorted on the key, so in your case they will be:

    1: "4","Test3"
    2: "10","Test"
    3: "13","Test2"
    4: "34","Test5"
    5: "66","Test4"

    That's the order you'll get them out if you iterate over the map from begin() to end().
    Correct is better than fast. Simple is better than complex. Clear is better than cute. Safe is better than insecure.
    --
    Sutter and Alexandrescu, C++ Coding Standards

    Programs must be written for people to read, and only incidentally for machines to execute.

    --
    Harold Abelson and Gerald Jay Sussman

    The cheapest, fastest and most reliable components of a computer system are those that aren't there.
    -- Gordon Bell


  3. #3
    Join Date
    Dec 2000
    Posts
    129
    Sorry I blew it, I am not thinking strait (long day) and this is the last problem on a long, LONG project. I think I am loosing my mind over this.

    Forget the example. Lets start over.

    I have a map that looks more like this:

    1: "050", "Place 1"
    2: "054", "Place 2"
    3: "060", "Place 3"
    4: "075", "Place 4"
    5: "75A", "Place 5"
    6: "75C", "Place 6"

    Then the entire map is pushed into 2 different combo boxes with the emelments combined (ie "050 - Place 1"), to allow them to select a start and an end. Then when they go to execute, their choices are checked against the map. If they are valid, then the next step is to ensure that the start and the end are actually correct and not reversed. So if they picked start = 75A and end = 050 it will not blow up the next part.

    So what I was wondering is how to tell which position in the map an element resides, because that would make for a quick compare.

    Or maybe of a better way to check the start and end.

    I thought of converting them to numbers and doing the compare, but the problem I have are the ones that have letters.

  4. #4
    Join Date
    Apr 2002
    Location
    India
    Posts
    26
    It's not related to your map probelm. but as i know, you can assosciated a nuber with each index of your combo box. So, check that index data. if that is less than mens that is in revers order. Or, If your pushing the same data in the both combo box. So, check that second combox box's index shoud not be less that the first index.

  5. #5
    Join Date
    Aug 2002
    Location
    Madrid
    Posts
    4,588
    Originally posted by Technocrat
    So what I was wondering is how to tell which position in the map an element resides, because that would make for a quick compare.
    There is no way to do that quickly with std::map.

    I would consider using two data structures (or using Saurabh's solution would be OK too):
    - a vector for holding the elements vector<pair<string, string> >
    - a map which is sort of an index into the vector: map<string, int> which maps a string like "75A" to 4, since element 4 of the vector would be ("75A", "Place 5").
    Get this small utility to do basic syntax highlighting in vBulletin forums (like Codeguru) easily.
    Supports C++ and VB out of the box, but can be configured for other languages.

  6. #6
    Join Date
    Dec 2000
    Posts
    129
    I was just think that it would be one less set to do since I already have to check if their choice was in the map. But it appears that is not a vaild option.

    Thanks for the idea Yves but I think it would just be quick just to look up the index like Saurabh suggested.

    Thanks for you help.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured