Connecting Tech Pros Worldwide Forums | Help | Site Map

STL map--> sort by value?

Jae
Guest
 
Posts: n/a
#1: Oct 21 '06
Hi

I wonder how can I implement the STL map sorting by value.

For example, I have a map m

map<int, intm;
m[1] = 10;
m[2] = 5;
m[4] = 6;
m[6] = 1

and then.. I'd like to sort that with the m's value.
So, if I print the map, I'd like to get the result like

m[6] = 1
m[2] = 5
m[4] = 6
m[1] = 10

this.

How can I sort like this way?
Is there any way that I can deal with the key and value with sorted values?





Rolf Magnus
Guest
 
Posts: n/a
#2: Oct 21 '06

re: STL map--> sort by value?


Jae wrote:
Quote:
Hi
>
I wonder how can I implement the STL map sorting by value.
Maps are always sorted by key. That's a requirement of the algorithm that
makes maps so fast for searching by key.
Quote:
For example, I have a map m
>
map<int, intm;
m[1] = 10;
m[2] = 5;
m[4] = 6;
m[6] = 1
>
and then.. I'd like to sort that with the m's value.
So, if I print the map, I'd like to get the result like
>
m[6] = 1
m[2] = 5
m[4] = 6
m[1] = 10
>
this.
>
How can I sort like this way?
Is there any way that I can deal with the key and value with sorted
values?
Well, why don't you swap the key with the value? Then the map is sorted just
like you want it.

Jae
Guest
 
Posts: n/a
#3: Oct 21 '06

re: STL map--> sort by value?


"Rolf Magnus" <ramagnus@t-online.dewrote in message
news:ehdoqs$6h9$03$1@news.t-online.com...
Quote:
Jae wrote:
>
Quote:
>Hi
>>
>I wonder how can I implement the STL map sorting by value.
>
Maps are always sorted by key. That's a requirement of the algorithm that
makes maps so fast for searching by key.
>
Quote:
>For example, I have a map m
>>
>map<int, intm;
>m[1] = 10;
>m[2] = 5;
>m[4] = 6;
>m[6] = 1
>>
>and then.. I'd like to sort that with the m's value.
>So, if I print the map, I'd like to get the result like
>>
>m[6] = 1
>m[2] = 5
>m[4] = 6
>m[1] = 10
>>
>this.
>>
>How can I sort like this way?
>Is there any way that I can deal with the key and value with sorted
>values?
>
Well, why don't you swap the key with the value? Then the map is sorted
just
like you want it.
>
Well. Because I have a case that

m[1] = 10;
m[3] = 10;

like this. If I swap it, then m[10] cannot store the m[1] and m[3] both.
That's the problem..


David Harmon
Guest
 
Posts: n/a
#4: Oct 21 '06

re: STL map--> sort by value?


On Sat, 21 Oct 2006 11:33:11 -0700 in comp.lang.c++, "Jae"
<suejlee@stanford.eduwrote,
Quote:
>Well. Because I have a case that
>
>m[1] = 10;
>m[3] = 10;
>
>like this. If I swap it, then m[10] cannot store the m[1] and m[3] both.
Then you would need std::multimap;


Salt_Peter
Guest
 
Posts: n/a
#5: Oct 21 '06

re: STL map--> sort by value?



Jae wrote:
Quote:
"Rolf Magnus" <ramagnus@t-online.dewrote in message
news:ehdoqs$6h9$03$1@news.t-online.com...
Quote:
Jae wrote:
Quote:
Hi
>
I wonder how can I implement the STL map sorting by value.
Maps are always sorted by key. That's a requirement of the algorithm that
makes maps so fast for searching by key.
Quote:
For example, I have a map m
>
map<int, intm;
m[1] = 10;
m[2] = 5;
m[4] = 6;
m[6] = 1
>
and then.. I'd like to sort that with the m's value.
So, if I print the map, I'd like to get the result like
>
m[6] = 1
m[2] = 5
m[4] = 6
m[1] = 10
>
this.
>
How can I sort like this way?
Is there any way that I can deal with the key and value with sorted
values?
Well, why don't you swap the key with the value? Then the map is sorted
just
like you want it.
>
Well. Because I have a case that
>
m[1] = 10;
m[3] = 10;
>
like this. If I swap it, then m[10] cannot store the m[1] and m[3] both.
That's the problem..
Then obviously you can't use a map. Try a multimap perhaps.

Jae
Guest
 
Posts: n/a
#6: Oct 21 '06

re: STL map--> sort by value?



"Salt_Peter" <pj_hern@yahoo.comwrote in message
news:1161456769.408304.198780@f16g2000cwb.googlegr oups.com...
Quote:
>
Jae wrote:
Quote:
>"Rolf Magnus" <ramagnus@t-online.dewrote in message
>news:ehdoqs$6h9$03$1@news.t-online.com...
Quote:
Jae wrote:
>
>Hi
>>
>I wonder how can I implement the STL map sorting by value.
>
Maps are always sorted by key. That's a requirement of the algorithm
that
makes maps so fast for searching by key.
>
>For example, I have a map m
>>
>map<int, intm;
>m[1] = 10;
>m[2] = 5;
>m[4] = 6;
>m[6] = 1
>>
>and then.. I'd like to sort that with the m's value.
>So, if I print the map, I'd like to get the result like
>>
>m[6] = 1
>m[2] = 5
>m[4] = 6
>m[1] = 10
>>
>this.
>>
>How can I sort like this way?
>Is there any way that I can deal with the key and value with sorted
>values?
>
Well, why don't you swap the key with the value? Then the map is sorted
just
like you want it.
>
>>
>Well. Because I have a case that
>>
>m[1] = 10;
>m[3] = 10;
>>
>like this. If I swap it, then m[10] cannot store the m[1] and m[3] both.
>That's the problem..
>
Then obviously you can't use a map. Try a multimap perhaps.
>
Can I sort the multimap's value?


Daniel T.
Guest
 
Posts: n/a
#7: Oct 21 '06

re: STL map--> sort by value?


"Jae" <suejlee@stanford.eduwrote:
Quote:
Hi
>
I wonder how can I implement the STL map sorting by value.
>
For example, I have a map m
>
map<int, intm;
m[1] = 10;
m[2] = 5;
m[4] = 6;
m[6] = 1
>
and then.. I'd like to sort that with the m's value.
So, if I print the map, I'd like to get the result like
>
m[6] = 1
m[2] = 5
m[4] = 6
m[1] = 10
>
this.
>
How can I sort like this way?
Is there any way that I can deal with the key and value with sorted values?
The easiest way is to put them in a set.

typedef map<int, intMyMap;

struct LessRight
{
bool operator()( const MyMap::value_type& lhs,
const MyMap::value_type& rhs ) const
{
return lhs.second < rhs.second;
}
};

typedef set< MyMap::value_type, LessRight MySet;

ostream& operator<<( ostream& os, const MySet::value_type& v ) {
return os << "m[" << v.first << "] = " << v.second;
}

int main() {
MyMap m;
m[1] = 10;
m[2] = 5;
m[4] = 6;
m[6] = 1;

MySet s( m.begin(), m.end() );
MySet::iterator it = s.begin();
while ( it != s.end() ) {
cout << *it++ << '\n';
}
}

--
There are two things that simply cannot be doubted, logic and perception.
Doubt those, and you no longer*have anyone to discuss your doubts with,
nor any ability to discuss them.
Daniel T.
Guest
 
Posts: n/a
#8: Oct 21 '06

re: STL map--> sort by value?


Rolf Magnus <ramagnus@t-online.dewrote:
Quote:
Jae wrote:
>
Quote:
Hi

I wonder how can I implement the STL map sorting by value.
>
Maps are always sorted by key. That's a requirement of the algorithm that
makes maps so fast for searching by key.
>
Quote:
For example, I have a map m

map<int, intm;
m[1] = 10;
m[2] = 5;
m[4] = 6;
m[6] = 1

and then.. I'd like to sort that with the m's value.
So, if I print the map, I'd like to get the result like

m[6] = 1
m[2] = 5
m[4] = 6
m[1] = 10

this.

How can I sort like this way?
Is there any way that I can deal with the key and value with sorted
values?
>
Well, why don't you swap the key with the value? Then the map is sorted just
like you want it.
You might have to use a multimap in that case.

--
There are two things that simply cannot be doubted, logic and perception.
Doubt those, and you no longer have anyone to discuss your doubts with,
nor any ability to discuss them.
LR
Guest
 
Posts: n/a
#9: Oct 22 '06

re: STL map--> sort by value?


Jae wrote:
Quote:
Hi
>
I wonder how can I implement the STL map sorting by value.
>
For example, I have a map m
>
map<int, intm;
m[1] = 10;
m[2] = 5;
m[4] = 6;
m[6] = 1
>
and then.. I'd like to sort that with the m's value.
So, if I print the map, I'd like to get the result like
>
m[6] = 1
m[2] = 5
m[4] = 6
m[1] = 10
>
this.
>
How can I sort like this way?
I'm not 100% certain I understand what you mean by sort in this context.

Quote:
Is there any way that I can deal with the key and value with sorted values?
What kind of container do you want to sort them in?

Could this possibly be what you're looking for?

-------------------------------------------------------------------
#include <iostream>
#include <map>
#include <utility>

typedef std::map<int,intNormalMap;
typedef std::map< std::pair<int,int>, NormalMap::const_iterator >
ReverseMap;

int main() {
NormalMap m;
m[1] = 10;
m[2] = 5;
m[4] = 6;
m[6] = 1;
m[11] = 10;
// note second '10' value,
// but all keys are still unique

// if the NormalMap is going to change
// often in the code, then this might
// be better in a function
ReverseMap n;
for(NormalMap::const_iterator i=m.begin(); i!=m.end(); i++) {
// note that key,value has become
// value, key here
// each of these pairs will be
// unique, because the key
// from the NormalMap is
// unique
n[ std::make_pair(i->second,i->first) ] = i;
}

// the ReverseMap is ordered by the value,key from the
// NormalMap, and its mapped_type is a const_iterator
// to the NormalMap
for(ReverseMap::const_iterator i=n.begin(); i!=n.end(); i++) {
NormalMap::const_iterator j = i->second;
std::cout << "m[" << j->first
<< "] = " << j->second << ";"
<< std::endl;
}
}
--------------------------------------------------

And the output was:

-----------------------------
m[6] = 1;
m[2] = 5;
m[4] = 6;
m[1] = 10;
m[11] = 10;
------------------------------

You could also put the data from the NormalMap into a std::set,
reversing the key and value.

LR
Salt_Peter
Guest
 
Posts: n/a
#10: Oct 22 '06

re: STL map--> sort by value?



Jae wrote:
Quote:
"Salt_Peter" <pj_hern@yahoo.comwrote in message
news:1161456769.408304.198780@f16g2000cwb.googlegr oups.com...
Quote:

Jae wrote:
Quote:
"Rolf Magnus" <ramagnus@t-online.dewrote in message
news:ehdoqs$6h9$03$1@news.t-online.com...
Jae wrote:

Hi
>
I wonder how can I implement the STL map sorting by value.

Maps are always sorted by key. That's a requirement of the algorithm
that
makes maps so fast for searching by key.

For example, I have a map m
>
map<int, intm;
m[1] = 10;
m[2] = 5;
m[4] = 6;
m[6] = 1
>
and then.. I'd like to sort that with the m's value.
So, if I print the map, I'd like to get the result like
>
m[6] = 1
m[2] = 5
m[4] = 6
m[1] = 10
>
this.
>
How can I sort like this way?
Is there any way that I can deal with the key and value with sorted
values?

Well, why don't you swap the key with the value? Then the map is sorted
just
like you want it.

>
Well. Because I have a case that
>
m[1] = 10;
m[3] = 10;
>
like this. If I swap it, then m[10] cannot store the m[1] and m[3] both.
That's the problem..
Then obviously you can't use a map. Try a multimap perhaps.
>
Can I sort the multimap's value?
no, unless the value itself (or a component thereof), is the key. A
multimap, like most associative containers, use a key to sort its
elements at insertion based on a predicate. In other words, its
impossible to have an associative container that is not sorted. The
question is: how is it sorted?
Answer: a key + a predicate. What the key is and how the predicate
sorts those keys is completely programmeable.

So if a map is a sorted container of element-pairs <key, valuewhere
all the keys are unique but some values may have duplicates, and you
need to sort by values (with duplicates), you'ld swap the map with a
multimap in order to allow for the duplicate keys. The map's value
becomes the multimap's key. Of course, swapping a map-element's value
to become a multimap-element's key is something you'll need to do
programatically, but thats a minor issue.

Note that even the multimap's duplicate keys themselves can be sorted
if that predicate is appropriately written.

Closed Thread