Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > sorting map value based

Reply
Thread Tools

sorting map value based

 
 
Philipp Kraus
Guest
Posts: n/a
 
      11-02-2010
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

 
Reply With Quote
 
 
 
 
Juha Nieminen
Guest
Posts: n/a
 
      11-02-2010
Philipp Kraus <(E-Mail Removed)> 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.
 
Reply With Quote
 
 
 
 
Philipp Kraus
Guest
Posts: n/a
 
      11-02-2010
On 2010-11-02 14:36:25 +0100, Sherm Pendley said:

> Philipp Kraus <(E-Mail Removed)> 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

 
Reply With Quote
 
LR
Guest
Posts: n/a
 
      11-02-2010
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...tml/index.html

LR
 
Reply With Quote
 
Joshua Maurice
Guest
Posts: n/a
 
      11-03-2010
On Nov 2, 7:48*am, Philipp Kraus <(E-Mail Removed)> 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.
 
Reply With Quote
 
Jorgen Grahn
Guest
Posts: n/a
 
      11-07-2010
On Tue, 2010-11-02, Sherm Pendley wrote:
> Philipp Kraus <(E-Mail Removed)> 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 .
 
Reply With Quote
 
Juha Nieminen
Guest
Posts: n/a
 
      11-07-2010
Jorgen Grahn <(E-Mail Removed)> 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.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Sorting keys of hash based on value Aldric Giacomoni Ruby 4 03-22-2010 08:35 PM
initial value of pointer value in std map ? PGK C++ 1 04-02-2009 10:46 AM
Sorting a hash table based on value (not key) vikas C Programming 3 08-16-2007 12:53 PM
Sorting based on string value Ninja67 Perl Misc 7 12-05-2006 05:41 PM
Sorting a map by value Kevin W. C++ 9 08-25-2004 11:19 AM



Advertisments