472,962 Members | 2,386 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

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

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

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;
  6. int main(){
  8.   // some data
  9.   map<string, double> myList;
  10.   myList["peter"]= 3.0;
  11.   myList["paul"]= 1.0;
  12.   myList["mary"] = 2.0;
  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.     }
  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";
  33. }

And it works as I expect it with g++ 3.4.4 cygwin, but I'm anxious since
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 2730
9,208 Expert Mod 8TB
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;
  3. bool Ascending(int first, int second)
  4. {
  5.     return first < second;
  6. }
  8. OR:
  10. map<int, string, Descending> myMap;
  12. bool Descending(int first, int second)
  13. {
  14.     return first > second;
  15. }
Dec 11 '07 #2
ok, got it, thank you
Dec 12 '07 #3

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

Similar topics

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)...
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...
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...
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)...
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...
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...
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...
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;
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;
by: DJRhino | last post by:
Was curious if anyone else was having this same issue or not.... I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 4 Oct 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
by: Aliciasmith | last post by:
In an age dominated by smartphones, having a mobile app for your business is no longer an option; it's a necessity. Whether you're a startup or an established enterprise, finding the right mobile app...
by: giovanniandrean | last post by:
The energy model is structured as follows and uses excel sheets to give input data: 1-Utility.py contains all the functions needed to calculate the variables and other minor things (mentions...
by: NeoPa | last post by:
Introduction For this article I'll be using a very simple database which has Form (clsForm) & Report (clsReport) classes that simply handle making the calling Form invisible until the Form, or all...
by: Teri B | last post by:
Hi, I have created a sub-form Roles. In my course form the user selects the roles assigned to the course. 0ne-to-many. One course many roles. Then I created a report based on the Course form and...
by: NeoPa | last post by:
Introduction For this article I'll be focusing on the Report (clsReport) class. This simply handles making the calling Form invisible until all of the Reports opened by it have been closed, when it...
by: isladogs | last post by:
The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, Mike...
by: GKJR | last post by:
Does anyone have a recommendation to build a standalone application to replace an Access database? I have my bookkeeping software I developed in Access that I would like to make available to other...

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.