CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2
  1. #1
    Join Date
    May 1999
    Location
    Saint Paul, Minnesota, US
    Posts
    91

    Sorting data using std::map or std::multimap

    Hi ...

    Happy New Year. Hope everyone is ok. I have not been here for a while.

    I have some contact lens data I'm trying to sort. I have some sort rules are static (hard-coded) followed by other rules form 'normal' rules

    Here are the requirements:

    1. First sort by Company and Product is a static order (hard-coded, non alphbetic)

    Sample
    o Company ProductName
    --------------------------------------------------------------------
    1. Ciba 1. Focus, 2. AirOptics, 3. Dailies, 4. then alphabetical.
    2. Bausch & Lomb 1. PureVision, 2. LiquidEye, 3. Softlens then alphabetical.

    2. Followed by Package size decending (80, 30 ...)

    3. Followed by Base Curve decending (8.6, 8,0 ...)

    4. Followed by Sphere decending (-0.5, 0.0, 1.0 ...)

    In the end the result might look like the following

    Sample data
    CO PR PS BC Sp
    -------------------------------
    Ciba Focus 10 8.6 0.00
    Ciba Focus 20 8.6 -0.25
    Ciba Focus 20 8.6 -0.10
    Ciba Focus 20 8.6 +0.25
    Ciba Air 10 8.6 -0.25
    Ciba Air 10 8.6 -0.10
    Ciba Air 20 8.1 -0.25
    Ciba Air 20 8.1 -0.10
    Ciba Air 20 8.1 0.00
    Ciba Air 20 8.6 +0.25
    Ciba Air 20 8.6 +0.40


    The data is contained in a struct with other data. The stuct is used in a map with a key. I am building a key to sort for the static requirments such as below.

    The key contains a prefix at the front of the key to force the static sort (non alphabetical), followed by a suffix to make the key unique. Sample key: "1_1_Ciba_Focus Monthlys_4"

    My plan to sort the other requirments is to take each of the other requirments (PackageSize, Base Curve and Sphere) and sort them against the previous element with the key.

    Since I will be sorting the following agaist the Package Size (10, 20 ...) - maybe I should be using the multimap, as it might be easter to sort this stuff without having the unique ID at the end of key ??

    With the key of "1_1_Ciba Focus" I would sort on the PackSize and then set the key to produce the correct sort

    start: (std::multimap)
    key: 1_1_Ciba Focus"
    Sample data
    CO PR Key
    -------------------------------
    Ciba Focus 1_1_Ciba Focus
    Ciba Focus 1_1_Ciba Focus
    Ciba Focus 1_1_Ciba Focus
    Ciba Focus 1_1_Ciba Focus

    Sample data
    CO PR PS Key
    -------------------------------
    Ciba Focus 10 1_1_1_Ciba Focus
    Ciba Focus 20 1_1_2_Ciba Focus
    Ciba Focus 20 1_1_2_Ciba Focus
    Ciba Focus 20 1_1_2_Ciba Focus


    Thanks,
    Chris
    chris@macgowan.com

  2. #2
    Join Date
    Jul 2005
    Location
    Netherlands
    Posts
    2,042

    Re: Sorting data using std::map or std::multimap

    Why use a map if you need to generate the key? Why not just use a set and implement a predicate for the sorting?
    Cheers, D Drmmr

    Please put [code][/code] tags around your code to preserve indentation and make it more readable.

    As long as man ascribes to himself what is merely a posibility, he will not work for the attainment of it. - P. D. Ouspensky

Tags for this Thread

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