sorting map value based

P

Philipp Kraus

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
 
J

Juha Nieminen

Philipp Kraus said:
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.
 
P

Philipp Kraus

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
 
L

LR

Philipp said:
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
 
J

Joshua Maurice

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.
 
J

Juha Nieminen

Jorgen Grahn said:
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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top