sorting map value based

Discussion in 'C++' started by Philipp Kraus, Nov 2, 2010.

  1. Hi,

    I try to sort a std::map on their values. My code:
    std::map<std::string, std::size_t> y;

    add some data to y and sorting with

    std::sort(y.begin(), y.end(), y.value_comp());

    I get an error "no match for operator+ in __first+1" in the stl_algo.h
    I would like to read my map in a sorted (descnding) order based on the
    values. How I can do this in the right way?

    Thanks

    Phil
    Philipp Kraus, Nov 2, 2010
    #1
    1. Advertising

  2. Philipp Kraus <> wrote:
    > I try to sort a std::map on their values. My code:
    > std::map<std::string, std::size_t> y;


    You don't sort maps. Maps are already internally sorted, and this
    order cannot be changed afterwards.

    > add some data to y and sorting with
    >
    > std::sort(y.begin(), y.end(), y.value_comp());
    >
    > I get an error "no match for operator+ in __first+1" in the stl_algo.h
    > I would like to read my map in a sorted (descnding) order based on the
    > values. How I can do this in the right way?


    If you want the elements ordered in a different way, you will have to
    copy the elements, or iterators to the elements, somewhere else, eg. to a
    vector, and then sort that.
    Juha Nieminen, Nov 2, 2010
    #2
    1. Advertising

  3. On 2010-11-02 14:36:25 +0100, Sherm Pendley said:

    > Philipp Kraus <> writes:
    >
    >> I try to sort a std::map on their values. My code:
    >> std::map<std::string, std::size_t> y;
    >>
    >> add some data to y and sorting with
    >>
    >> std::sort(y.begin(), y.end(), y.value_comp());
    >>
    >> I get an error "no match for operator+ in __first+1" in the stl_algo.h
    >> I would like to read my map in a sorted (descnding) order based on the
    >> values. How I can do this in the right way?

    >
    > A map is unordered by nature, and can't be sorted - that's why you're
    > getting that error. You need to extract the values from the map, store
    > them into a container type (such as a vector) that *can* be sorted,
    > then sort that.


    I need always both key => value and value => key, but in the first case
    key orderd and in the the second value ordered. My example:

    key1 => 123
    key2 => 50
    key3 => 100
    key4 => 10
    key5 => 50

    This is the content of my map, now I need the same data but in this order
    123 => key1
    100 => key3
    50 => key5
    50 => key2
    10 => key4

    For geting the second order must I copy the complete map data into the
    multimap or is there a better structure?

    Thanks

    Phil
    Philipp Kraus, Nov 2, 2010
    #3
  4. Philipp Kraus

    LR Guest

    Philipp Kraus wrote:

    > I need always both key => value and value => key, but in the first case
    > key orderd and in the the second value ordered. My example:
    >
    > key1 => 123
    > key2 => 50
    > key3 => 100
    > key4 => 10
    > key5 => 50
    >
    > This is the content of my map, now I need the same data but in this order
    > 123 => key1
    > 100 => key3
    > 50 => key5
    > 50 => key2
    > 10 => key4
    >
    > For geting the second order must I copy the complete map data into the
    > multimap or is there a better structure?


    Have you looked into boost's bimap?
    http://www.boost.org/doc/libs/1_44_0/libs/bimap/doc/html/index.html

    LR
    LR, Nov 2, 2010
    #4
  5. On Nov 2, 7:48 am, Philipp Kraus <> wrote:
    > I need always both key => value and value => key,


    I don't know what the means.

    For the moment, let's takes the (key, value) pairs and consider it to
    just be one object, albeit with different possible sort rules, such as
    "Sort only by the key" and "Sort only by the second element of the
    pair (the value)". If you need two maintain two different sort orders
    at the same time, then a single std::set and std:map will not let you
    do this. Each std::set and std::map object have an specific individual
    unchanging sort order over its lifetime. If you need the objects (the
    pairs) sorted in two different sort orders, then you would need two
    different std::set and/or std::map objects, or to use a different data
    structure.

    If you wanted to use two different std::set or std::map objects, then
    you would not need to copy the data. One could simply be a set of
    pointers, and you override the sort rule to sort based on pointed-to
    values, not the pointer values, ex:
    #include <set>
    struct int_ptr_comp{ bool operator() (int* x, int* y) { return *x <
    *y; } };
    std::set<int*, int_ptr_comp> some_set;
    It would be up to you to ensure that the two different std::set or
    std::map objects remain in sync. This could be done with a simple
    class and abstraction, data hiding, and encapsulation.

    Alternatively use some Boost container which does this all for you
    already (or perhaps uses a more intelligent data structure), as
    suggested else-thread.
    Joshua Maurice, Nov 3, 2010
    #5
  6. Philipp Kraus

    Jorgen Grahn Guest

    On Tue, 2010-11-02, Sherm Pendley wrote:
    > Philipp Kraus <> writes:
    >
    >> I try to sort a std::map on their values. My code:
    >> std::map<std::string, std::size_t> y;
    >>
    >> add some data to y and sorting with
    >>
    >> std::sort(y.begin(), y.end(), y.value_comp());

    ....

    > A map is unordered by nature, and can't be sorted


    No -- a map is /ordered/ by nature, and cannot be re-ordered.

    /Jorgen

    --
    // Jorgen Grahn <grahn@ Oo o. . .
    \X/ snipabacken.se> O o .
    Jorgen Grahn, Nov 7, 2010
    #6
  7. Jorgen Grahn <> wrote:
    > On Tue, 2010-11-02, Sherm Pendley wrote:
    >> A map is unordered by nature, and can't be sorted

    >
    > No -- a map is /ordered/ by nature, and cannot be re-ordered.


    In fact, a map is guaranteed to be ordered such that when you traverse
    it from begin() to end(), you get the elements in the order defined by
    the element comparator.
    Juha Nieminen, Nov 7, 2010
    #7
    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. Kevin W.

    Sorting a map by value

    Kevin W., Aug 24, 2004, in forum: C++
    Replies:
    9
    Views:
    35,294
    Richard Herring
    Aug 25, 2004
  2. vikas
    Replies:
    3
    Views:
    508
    CBFalconer
    Aug 16, 2007
  3. PGK
    Replies:
    1
    Views:
    515
  4. Aldric Giacomoni

    Sorting keys of hash based on value

    Aldric Giacomoni, Mar 22, 2010, in forum: Ruby
    Replies:
    4
    Views:
    135
    Robert Klemme
    Mar 22, 2010
  5. Ninja67

    Sorting based on string value

    Ninja67, Dec 4, 2006, in forum: Perl Misc
    Replies:
    7
    Views:
    112
    Ninja67
    Dec 5, 2006
Loading...

Share This Page