473,587 Members | 2,580 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

can I rely on the sorting of std::map<>

63 New Member
I have some data in a map and I want to sort it. Currently I have implemented it like this:


Expand|Select|Wrap|Line Numbers
  1. #include <iostream>
  2. #include <map>
  3. #include <string>
  4. using namespace std;
  5.  
  6. int main(){
  7.  
  8.   // some data
  9.   map<string, double> myList;
  10.   myList["peter"]= 3.0;
  11.   myList["paul"]= 1.0;
  12.   myList["mary"] = 2.0;
  13.  
  14.   // sort the elements
  15.   map<double, string> mySortedList;
  16.   for ( map<string, double>::iterator i = myList.begin();
  17.     i != myList.end();
  18.     i++ )
  19.     {
  20.       mySortedList[ i->second ] = i->first;
  21.     }
  22.  
  23.   // print the sorted elements
  24.   cout << "sorted: ";
  25.   for ( map<double, string>::iterator i = mySortedList.begin();
  26.     i != mySortedList.end();
  27.     i++ )
  28.     {
  29.       cout << i->first << " {" << i->second << "} ";
  30.     }
  31.   cout << "\n";
  32.  
  33. }
  34.  

And it works as I expect it with g++ 3.4.4 cygwin, but I'm anxious since
http://www.cppreferenc e.com/cppmap/index.html
only says that maps are sorted, but not whether that is ascending or descending or whatever-map-blackmagic-sorting.

Anybody able to clarify this?
Dec 11 '07 #1
2 2776
weaknessforcats
9,208 Recognized Expert Moderator Expert
maps are sorted.

They are sorted by the operator< of the class used as the key.

If there is no operator< for the class used as a key, you can write an operator< that takes two key valus as argument and returns a bool.

OR, you can write any function that takes two key objects, compares them and returns a bool.

The sequence you get is determined by the comparision function and not by the map.

The map template has:

map<key, value, func>

where key is the key object, valus is the object associated with the key, and func is a binary predicate. In STLspeak, a binary predicate is a function thay takes two arguments and returns a bool. For example, yo use any old compare function, you could:

Expand|Select|Wrap|Line Numbers
  1. map<int, string, Ascending> myMap;
  2.  
  3. bool Ascending(int first, int second)
  4. {
  5.     return first < second;
  6. }
  7.  
  8. OR:
  9.  
  10. map<int, string, Descending> myMap;
  11.  
  12. bool Descending(int first, int second)
  13. {
  14.     return first > second;
  15. }
  16.  
Dec 11 '07 #2
jabbah
63 New Member
ok, got it, thank you
Dec 12 '07 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

5
8731
by: Peter Jansson | last post by:
Hello, I have the following code: std::map<int,std::set<std::string> > k; k="1234567890"; k="2345678901"; //... std::set<std::string> myMethod(std::map<int,std::set<std::string> > k) throw(std::runtime_error)
13
4598
by: jstanforth | last post by:
This is probably a very obvious question, but I'm not clear on what operators need to be implemented for std::map.find() to work. For example, I have a class MyString that wraps std::string, and which also implements ==, <, <=, >, >=, etc. (Those operators are tested and working correctly.) If I assign map = "world", it saves the...
19
6132
by: Erik Wikström | last post by:
First of all, forgive me if this is the wrong place to ask this question, if it's a stupid question (it's my second week with C++), or if this is answered some place else (I've searched but not found anything). Here's the problem, I have two sets of files, the name of a file contains a number which is unique for each set but it's possible...
1
3718
by: Maxwell | last post by:
Hello, I having having oodles of trouble using the std lib in my MC++ (VS.NET 2003) Class library. I figured out a simple sample to reproduce the errors I am having. Create a MC++ (VS.NET 2003) class library and type in the following code below: #include <map> #include<string>
10
2954
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...
2
2244
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...
3
2936
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...
12
5819
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;
6
5418
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
8206
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. ...
0
8340
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...
1
7967
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...
0
6621
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...
1
5713
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5392
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...
0
3840
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...
0
3875
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2353
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

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.