sorting maps according to their value_type

Discussion in 'C++' started by cesco, Feb 10, 2006.

  1. cesco

    cesco Guest

    I'm designing an algorithm with maps that need the elements to be
    sorted by value instead of by key. Does anyone have a suggestion on how
    to do that?

    #include <map>
    using namespace std;

    int main()
    {
    // how can I specify a SortingCriterion based on the value so that
    // the months are ordered from the one with largest number of day
    // to the one with smallest?
    map<const char*, int /*, SortingCriterion*/ > months;

    months["february"] = 28;
    months["march"] = 31;
    months["april"] = 30;

    }

    Thanks & regards
    Francesco
    cesco, Feb 10, 2006
    #1
    1. Advertising

  2. cesco

    Ben Pope Guest

    cesco wrote:
    > I'm designing an algorithm with maps that need the elements to be
    > sorted by value instead of by key. Does anyone have a suggestion on how
    > to do that?


    Why? It's sorted by key for quick lookups. If you sort by value, I
    suspect you'll find it hard to find what you're looking for.

    What about an iterator that traverses the sequence in the specified
    order? Or a different container?

    What problem do you need to solve such that the months should be day length?

    What about a set or pairs (string, int) with sorting criterion on .second?

    Ben Pope
    --
    I'm not just a number. To many, I'm known as a string...
    Ben Pope, Feb 10, 2006
    #2
    1. Advertising

  3. cesco

    cesco Guest

    Thanks for the reply.

    The reason why I need to sort them according to the value is that later
    on I apply the algorithm for_each (combined, of course, with a function
    object) starting from the first element that should have the highest
    value in the value_type field and going to the last. I'd like to avoid
    a vector of pairs because I'm reusing some code and in that case I may
    have to change a lot.

    Any other suggestion?

    Thanks again
    Francesco
    cesco, Feb 10, 2006
    #3
  4. cesco

    Ben Pope Guest

    cesco wrote:
    > Thanks for the reply.
    >
    > The reason why I need to sort them according to the value is that later
    > on I apply the algorithm for_each (combined, of course, with a function
    > object) starting from the first element that should have the highest
    > value in the value_type field and going to the last. I'd like to avoid
    > a vector of pairs because I'm reusing some code and in that case I may
    > have to change a lot.
    >
    > Any other suggestion?


    You didn't comment on my iterator idea. You can create your own
    iterator that iterates the sequence you require.

    In fact, if you use a std::set<std::pair<int, std::string> >, the
    ordering you require comes for free.

    Since these are static, you could also create a set that gives you the
    ordering you require, but still store the data in the map. Or a vector
    of std::map<std::string, int>::const_iterator that gives you the
    sequence you require...

    Ben Pope
    --
    I'm not just a number. To many, I'm known as a string...
    Ben Pope, Feb 10, 2006
    #4
  5. cesco

    Guest

    maybe something on the line of:

    map<int,float> externallist;

    class comparethem
    {
    public:
    map<int,float> *list;
    bool operator()(int a,int b){return (*list)[a]<(*list);}
    };

    void sortit(map<key,value> list)
    {
    comparethem cmp;
    cmp.list=&list;

    sort(list.begin(),list.end(),cmp);
    //put them into original

    }
    , Feb 10, 2006
    #5
  6. cesco wrote:
    > I'm designing an algorithm with maps that need the elements to be
    > sorted by value instead of by key. Does anyone have a suggestion on how
    > to do that?
    >
    > #include <map>
    > using namespace std;
    >
    > int main()
    > {
    > // how can I specify a SortingCriterion based on the value so that
    > // the months are ordered from the one with largest number of day
    > // to the one with smallest?
    > map<const char*, int /*, SortingCriterion*/ > months;
    >
    > months["february"] = 28;
    > months["march"] = 31;
    > months["april"] = 30;
    >
    > }


    Then just use a multimap with the number of days as the key. If you
    want two or more orders / indexes in your map use boost::multi_index
    instead.
    Maxim Yegorushkin, Feb 10, 2006
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Jean-Fran?ois Lacrampe

    <div> automatic width according to their contents?

    Jean-Fran?ois Lacrampe, Jun 24, 2004, in forum: HTML
    Replies:
    5
    Views:
    19,233
    suyashxp
    Oct 29, 2009
  2. Chyi Pin Lim

    value_type of a back_insert_iterator

    Chyi Pin Lim, Dec 5, 2003, in forum: C++
    Replies:
    9
    Views:
    511
    ppLiu_china
    Dec 11, 2003
  3. Simon Elliott
    Replies:
    4
    Views:
    1,142
    Simon Elliott
    Mar 10, 2005
  4. Marcus
    Replies:
    2
    Views:
    577
    Marcus
    Dec 9, 2005
  5. John Reye
    Replies:
    16
    Views:
    685
    Tim Rentsch
    Jun 23, 2012
Loading...

Share This Page