Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. ATL / WTL / STL
  4. sort method of a multimap

sort method of a multimap

Scheduled Pinned Locked Moved ATL / WTL / STL
algorithmshelpquestion
2 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • R Offline
    R Offline
    Rodolfo Lima
    wrote on last edited by
    #1

    Is there a way to guarantee that the values inside a multimap are sorted in a certain way? I don't mean just sorting the keys, but values mapped to the same key also sorted by a functor (function object) or anything else? Even though I can retrieve the value_comp functor, I cannot set it because it's defined within the multimap class. This default value_comp just sorts the keys. What I really want is that equal_range returns iterators to sorted values according to my compare functor, but I couldn't find a way to accomplish this. The the values returned by the iterators aren't sorted in any special way that I can contro. Thanks for any help, rod

    J 1 Reply Last reply
    0
    • R Rodolfo Lima

      Is there a way to guarantee that the values inside a multimap are sorted in a certain way? I don't mean just sorting the keys, but values mapped to the same key also sorted by a functor (function object) or anything else? Even though I can retrieve the value_comp functor, I cannot set it because it's defined within the multimap class. This default value_comp just sorts the keys. What I really want is that equal_range returns iterators to sorted values according to my compare functor, but I couldn't find a way to accomplish this. The the values returned by the iterators aren't sorted in any special way that I can contro. Thanks for any help, rod

      J Offline
      J Offline
      Joaquin M Lopez Munoz
      wrote on last edited by
      #2

      Well, you can have sort of what you're after by using multiset instead of multimap like the following: Suppose your multimap is originally defined as:

      std::multimap<type_a,type_b>

      So, define a struct holding the two parts of the multimap:

      struct pair_ab
      {
      type_a first; // names of members à la std::pair
      type_b second;

      bool operator<(const pair_ab& x)const
      {
      if(first<x.first)return true;
      if(x.first<first)return false;
      return second<x.second;
      }
      };

      and replace your multimap with

      std::multiset<pair_ab>

      Now, this multiset really behaves as a multimap on type_a with the added feature that elements with equivalent first members are further sorted by second. Please report back if this works. Good luck. Joaquín M López Muñoz Telefónica, Investigación y Desarrollo

      1 Reply Last reply
      0
      Reply
      • Reply as topic
      Log in to reply
      • Oldest to Newest
      • Newest to Oldest
      • Most Votes


      • Login

      • Don't have an account? Register

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • World
      • Users
      • Groups