Help | Site Map
Connecting Tech Pros Worldwide
 
 
LinkBack Thread Tools
  #1  
Old October 21st, 2006, 06:55 PM
Jae
Guest
 
Posts: n/a
Default STL map--> sort by value?

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?




  #2  
Old October 21st, 2006, 07:15 PM
Rolf Magnus
Guest
 
Posts: n/a
Default 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.

  #3  
Old October 21st, 2006, 07:25 PM
Jae
Guest
 
Posts: n/a
Default 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..


  #4  
Old October 21st, 2006, 07:35 PM
David Harmon
Guest
 
Posts: n/a
Default 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;


  #5  
Old October 21st, 2006, 07:35 PM
Salt_Peter
Guest
 
Posts: n/a
Default 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.

  #6  
Old October 21st, 2006, 08:05 PM
Jae
Guest
 
Posts: n/a
Default 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?


  #7  
Old October 21st, 2006, 08:25 PM
Daniel T.
Guest
 
Posts: n/a
Default 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.
  #8  
Old October 21st, 2006, 08:35 PM
Daniel T.
Guest
 
Posts: n/a
Default 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.
  #9  
Old October 22nd, 2006, 12:15 AM
LR
Guest
 
Posts: n/a
Default 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
  #10  
Old October 22nd, 2006, 03:25 AM
Salt_Peter
Guest
 
Posts: n/a
Default 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.

 

Bookmarks

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 Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over network members.
Post your question now . . .
It's fast and it's free

Popular Articles