473,396 Members | 1,789 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 map<T>?

Suppose I have a problem for which an associative array is the most
natural fit, but which I need to be able to output in sorted order of the
values.

STL map would seem like a good fit, except that maps are sorted on key,
not value.

I can picture a couple of approaches, but I'm not clear on what would be
cleanest.

Any ideas?

--
There are three kinds of men. The one that learns by reading. The few
who learn by observation. The rest of them have to pee on the electric
fence for themselves.
- Will Rogers

Mar 5 '07 #1
3 1823
Jeff Dege wrote:
Suppose I have a problem for which an associative array is the most
natural fit, but which I need to be able to output in sorted order of the
values.

STL map would seem like a good fit, except that maps are sorted on key,
not value.

I can picture a couple of approaches, but I'm not clear on what would be
cleanest.

Any ideas?
Well I suppose the fancy approach would be to have two sets, each
pointing to common data but with different sorting criteria. That way
you'd have key sorted data for lookup and value sorted data for output.
Wrap it all up in a class so things can't get out of step and away you go.

But most of the time I think I'd just copy to a vector, sort the vector
and then throw the vector away.

john
Mar 5 '07 #2
John Harrison wrote:
Jeff Dege wrote:
>Suppose I have a problem for which an associative array is the most
natural fit, but which I need to be able to output in sorted order of the
values.

STL map would seem like a good fit, except that maps are sorted on key,
not value.

I can picture a couple of approaches, but I'm not clear on what would be
cleanest.

Any ideas?

But most of the time I think I'd just copy to a vector, sort the vector
and then throw the vector away.
That would be my suggestion too. The only addition I'd make is that, if
the values are large enough that copying and sorting may be inefficient,
you can make a vector of pointers to the map values and sort that instead.
Mar 5 '07 #3
On 2007-03-05 10:07:54 -0800, Jeff Dege <jd***@jdege.visi.comsaid:
Suppose I have a problem for which an associative array is the most
natural fit, but which I need to be able to output in sorted order of the
values.

STL map would seem like a good fit, except that maps are sorted on key,
not value.

I can picture a couple of approaches, but I'm not clear on what would be
cleanest.

Any ideas?
I would probably do one of the following:

1) Copy the data into a vector and sort it, and output *that*, then
discard the vector when you're done.
2) Copy the data into a set, and output that ... (may be faster or
slower than copying to a vector depending on your data)
3) Maintain a set of map::iterator's that is sorted by data type, but
points into your map.
4) Maintian a second map, that contains the same data, but with the key
and value swapped.

I can imagine myself using any of the above methods depending on
performance and data constraints.
--
Clark S. Cox III
cl*******@gmail.com

Mar 5 '07 #4

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

Similar topics

1
by: sachin_mzn | last post by:
Hi, When we use find method over a STL map. which searching algorithm is used internally. Is it a hash search? Or map implementation internally decide depending on element. -Sachin
3
by: mcassiani | last post by:
Hi, I need use map faster as possible (I store in the map data about open network connections). First a question, this code fragment is from "The C++ Programming........
10
by: Szabolcs Horvát | last post by:
Consider the attached example program: an object of type 'A' is inserted into a 'map<int, Am;'. Why does 'm;' call the copy constructor of 'A' twice in addition to a constructor call? The...
2
by: brzozo2 | last post by:
Hello, this program might look abit long, but it's pretty simple and easy to follow. What it does is read from a file, outputs the contents to screen, and then writes them to a different file. It...
16
by: barcaroller | last post by:
I have a map<T*that stores pointers to objects. How can I tell map<T*to use the objects' operator<() and not the value of the pointers for sorting? If that's not feasible, what alternatives do...
3
by: newbie | last post by:
Same thing g++ complains when using hash_map<>, but is happy with map<--I understand hahs_map is not standardized, but since the compiler didn't complain something like 'hash_map<not defined', I...
2
by: DaTurk | last post by:
Hi, I'm trying to hold a map of ints,and function pointers in C++ map<int, (*functPtr)(int, int)something I need to hold a list of callbacks. For some reason this syntax is not working. Any...
12
by: jabbah | last post by:
Actually I'm quite sure I've missed something trivial here, but I just can't find it. Seemingly I cannot read from a const map& I try #include <iostream> #include <map> using namespace std;
2
by: jabbah | last post by:
I have some data in a map and I want to sort it. Currently I have implemented it like this: #include <iostream> #include <map> #include <string> using namespace std; int main(){
6
by: Juha Nieminen | last post by:
joseph cook wrote: Not always. By default, yes, but you can specify other comparators, eg: std::map<int, int, std::greaterreversedMap;
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...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
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...
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.