473,661 Members | 2,429 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

STL map--> sort by value?

Jae
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?


Oct 21 '06 #1
9 16939
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.

Oct 21 '06 #2
Jae
"Rolf Magnus" <ra******@t-online.dewrote in message
news:eh******** *****@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..
Oct 21 '06 #3
On Sat, 21 Oct 2006 11:33:11 -0700 in comp.lang.c++, "Jae"
<su*****@stanfo rd.eduwrote,
>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;
Oct 21 '06 #4

Jae wrote:
"Rolf Magnus" <ra******@t-online.dewrote in message
news:eh******** *****@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.

Oct 21 '06 #5
Jae

"Salt_Peter " <pj*****@yahoo. comwrote in message
news:11******** **************@ f16g2000cwb.goo glegroups.com.. .
>
Jae wrote:
>"Rolf Magnus" <ra******@t-online.dewrote in message
news:eh******* ******@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?
Oct 21 '06 #6
"Jae" <su*****@stanfo rd.eduwrote:
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_ty pe& lhs,
const MyMap::value_ty pe& rhs ) const
{
return lhs.second < rhs.second;
}
};

typedef set< MyMap::value_ty pe, LessRight MySet;

ostream& operator<<( ostream& os, const MySet::value_ty pe& 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.
Oct 21 '06 #7
Rolf Magnus <ra******@t-online.dewrote:
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.
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.
Oct 21 '06 #8
LR
Jae wrote:
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.

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,in tNormalMap;
typedef std::map< std::pair<int,i nt>, NormalMap::cons t_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::cons t_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
Oct 21 '06 #9

Jae wrote:
"Salt_Peter " <pj*****@yahoo. comwrote in message
news:11******** **************@ f16g2000cwb.goo glegroups.com.. .

Jae wrote:
"Rolf Magnus" <ra******@t-online.dewrote in message
news:eh******** *****@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.

Oct 22 '06 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
2687
by: Gunjan Garg | last post by:
Hello All, I am working to create a generic datagrid which accepts a datasource(ListData - This is our own datatype) and depending on the calling program customizes itself for sorting, paginantion or accepting the add and remove item events. What i am observing is that none of the vents are happening... (Sort, page, or item). I am sure I am missing something basic here... Need help... Thanks much
3
6187
by: Jim Adams | last post by:
I'm counting the frequency of word occurances and would like to return a key/value list sorted descending by frequency. So I need to quickly see if a word (key) is in the list, but later sort the list descending by values. Any ideas on how to do this efficiently? Thanks,
17
1600
by: parvus | last post by:
Hi, I´m searching a solution for the following problem: I´ve a generated XML Document like <....> <struct name="number" type="PartNumber">100</struct> <struct name="identity" type="PartIdentity">1</struct> <enum name="ex_identity" type="....."></enum> <....> Now i want to use a XSLT file to create a new structured XML file like
2
1183
by: keys4worship | last post by:
One of my users called yesterday and asked me to look into a problem on a datagrid. Everytime she clicked on a row to edit, another row would appear in it's place. After a little analysis it appeared that this only happened after the datagrid was sorted. I placed the row in edit mode. Almost everytime that I clicked the Cancel button on that row I experienced a random shuffling of rows. I am using ViewState and DataView Sort to hold the...
48
4456
by: Alex Chudnovsky | last post by:
I have come across with what appears to be a significant performance bug in ..NET 2.0 ArrayList.Sort method when compared with Array.Sort on the same data. Same data on the same CPU gets sorted a lot faster with both methods using .NET 1.1, that's why I am pretty sure its a (rather serious) bug. Below you can find C# test case that should allow you to reproduce this error, to run it you will need to put 2 data files into current directory...
15
2787
by: bcochofel | last post by:
Hi, I want to use a variable to sort elements. That var his passed with query string (I'm using Perl CGI to generate XML). Here's a sample of my output: --------------------------------------------- <?xml version="1.0" encoding="iso-8859-1"?> <?xml-stylesheet type="text/xml" href="RR.xsl"?> <!-- $Id: template.xml,v 1.5 2006/12/11 11:13:30 bcochofel Exp $ --> <RR xmlns:xsi="http://www.w3.org/2001/XMLSchema"...
0
1486
by: kgiraldin | last post by:
Hello, I have some code commented out in order to make this work. However, I would prefer to use the commented code so my users can SORT on the column heading because I know they are going to want to do that. I appreciate any help that you can give me. Thanks, Kris ----------------------------------------------------- <html> <body>
4
1093
by: am1234 | last post by:
Hello All: I have a datagrid in my C# windows application (2.0). I have a routine that is refreshing the datagrid, which is causing it to lose its preselected sort. When this refresh happens, I need to run a method to pull out the sort value so I can re-apply it after the refresh happens but I cannot find out how to pull the sort value from the datagrid. Can anyone help me with this? Thanks Andy
1
2022
by: VanZandt | last post by:
how do I get a cell value for selected qty? Please help!!! below is the code. Thanks, <?xml version = "1.0" encoding = "utf-8"?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html xmlns = "http://www.w3.org/1999/xhtml">
0
1451
by: Joe Cagg | last post by:
I have looked All over the Internet to try and find an example of How to fix the following. How to Sort a List in Desending Order by the "Value" and Sort the Duplicates by the "Key" ? Then Print out the Results in the format below. I have enclosed my code and it works, but the problem happens when there are duplicate values, which occurs when you use SortedList(). I would GREATLY APPRECIATE it if someone could PLEASE Modify this Code or...
0
8851
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8754
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8542
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8630
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7362
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5650
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4177
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
2760
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
1984
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.