473,396 Members | 1,846 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

sorting maps according to their value_type

I'm designing an algorithm with maps that need the elements to be
sorted by value instead of by key. Does anyone have a suggestion on how
to do that?

#include <map>
using namespace std;

int main()
{
// how can I specify a SortingCriterion based on the value so that
// the months are ordered from the one with largest number of day
// to the one with smallest?
map<const char*, int /*, SortingCriterion*/ > months;

months["february"] = 28;
months["march"] = 31;
months["april"] = 30;

}

Thanks & regards
Francesco

Feb 10 '06 #1
5 1576
cesco wrote:
I'm designing an algorithm with maps that need the elements to be
sorted by value instead of by key. Does anyone have a suggestion on how
to do that?


Why? It's sorted by key for quick lookups. If you sort by value, I
suspect you'll find it hard to find what you're looking for.

What about an iterator that traverses the sequence in the specified
order? Or a different container?

What problem do you need to solve such that the months should be day length?

What about a set or pairs (string, int) with sorting criterion on .second?

Ben Pope
--
I'm not just a number. To many, I'm known as a string...
Feb 10 '06 #2
Thanks for the reply.

The reason why I need to sort them according to the value is that later
on I apply the algorithm for_each (combined, of course, with a function
object) starting from the first element that should have the highest
value in the value_type field and going to the last. I'd like to avoid
a vector of pairs because I'm reusing some code and in that case I may
have to change a lot.

Any other suggestion?

Thanks again
Francesco

Feb 10 '06 #3
cesco wrote:
Thanks for the reply.

The reason why I need to sort them according to the value is that later
on I apply the algorithm for_each (combined, of course, with a function
object) starting from the first element that should have the highest
value in the value_type field and going to the last. I'd like to avoid
a vector of pairs because I'm reusing some code and in that case I may
have to change a lot.

Any other suggestion?


You didn't comment on my iterator idea. You can create your own
iterator that iterates the sequence you require.

In fact, if you use a std::set<std::pair<int, std::string> >, the
ordering you require comes for free.

Since these are static, you could also create a set that gives you the
ordering you require, but still store the data in the map. Or a vector
of std::map<std::string, int>::const_iterator that gives you the
sequence you require...

Ben Pope
--
I'm not just a number. To many, I'm known as a string...
Feb 10 '06 #4
maybe something on the line of:

map<int,float> externallist;

class comparethem
{
public:
map<int,float> *list;
bool operator()(int a,int b){return (*list)[a]<(*list)[b];}
};

void sortit(map<key,value> list)
{
comparethem cmp;
cmp.list=&list;

sort(list.begin(),list.end(),cmp);
//put them into original

}

Feb 10 '06 #5

cesco wrote:
I'm designing an algorithm with maps that need the elements to be
sorted by value instead of by key. Does anyone have a suggestion on how
to do that?

#include <map>
using namespace std;

int main()
{
// how can I specify a SortingCriterion based on the value so that
// the months are ordered from the one with largest number of day
// to the one with smallest?
map<const char*, int /*, SortingCriterion*/ > months;

months["february"] = 28;
months["march"] = 31;
months["april"] = 30;

}


Then just use a multimap with the number of days as the key. If you
want two or more orders / indexes in your map use boost::multi_index
instead.

Feb 10 '06 #6

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

Similar topics

7
by: Jorge Schramm | last post by:
Hi, a collegue of mine is trying to write a serialisable container (reads at construction, writes at destruction). The writing part is pretty easy: simply iterate through the container and...
43
by: Steven T. Hatton | last post by:
Now that I have a better grasp of the scope and capabilities of the C++ Standard Library, I understand that products such as Qt actually provide much of the same functionality through their own...
2
by: amolpan | last post by:
I am using a vector of maps to store a sequence of key-value pairs. I fear usage of an STL map would lead to serious memory bottlenecks. Instead I was thinking of using an object to store key-value...
22
by: mike | last post by:
If I had a date in the format "01-Jan-05" it does not sort properly with my sort routine: function compareDate(a,b) { var date_a = new Date(a); var date_b = new Date(b); if (date_a < date_b)...
25
by: Dan Stromberg | last post by:
Hi folks. Python appears to have a good sort method, but when sorting array elements that are very large, and hence have very expensive compares, is there some sort of already-available sort...
3
by: utab | last post by:
Dear all, I am trying to create a look-up table for a text search and replacement class which will be used with a commercial software(MCS NASTRAN, I guess you may have heard that before ). I...
5
by: pallav | last post by:
I have a map like this: typedef boost::shared_ptr<NodeNodePtr; typedef std::vector<NodePtrNodeVecPtr; typedef std::map<std::string, NodePtrNodeMap; typedef std::map<std:string,...
3
by: SneakyElf | last post by:
i am very green with c++ so i get stuck on very simple things anyway, i need to write a program that would read data from file (containing names of tv shows and their networks) one line at a time...
4
by: Barbiturico76 | last post by:
Hi guys, anybody knows if it is possible to generate a <div id="myMap" style="width: 400; height:300; left: 0px; top: 0px;" /> inside your intranet site, to represent a programmatically...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
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,...
0
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...
0
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...
0
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,...

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.