473,782 Members | 2,554 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 1835
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.vi si.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
1778
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
1437
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
2975
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 constructors and copy constructors in 'A' report when they are called. 'whoami' is just a unique identifier assigned to every object of type 'A'. The output of the program is: constructor 0 constructor 1
2
2261
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 uses map<and heavy overloading. The problem is, the output file differs from input, and for the love of me I can't figure out why ;p #include <iostream> #include <fstream> #include <sstream>
16
2007
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 I have to sort a set of objects that cannot and should not be copied (i.e. I can use only pointers to these objects)? Note: I am aware of the method of creating a new class that wraps the pointers of the objects and then provides its own...
3
2942
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 suppose it's supported and should behave well when I used it correctly. BUT it didn't. Here is my code snippet: class MyKey { public: virtual void foo() { return; }
2
1586
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 ideas?
12
5838
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
2789
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
5440
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
9639
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9474
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10308
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...
1
10076
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
9939
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
6729
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
5375
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...
2
3633
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2870
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.