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(){
-
-
// some data
-
map<string, double> myList;
-
myList["peter"]= 3.0;
-
myList["paul"]= 1.0;
-
myList["mary"] = 2.0;
-
-
// sort the elements
-
map<double, string> mySortedList;
-
for ( map<string, double>::iterator i = myList.begin();
-
i != myList.end();
-
i++ )
-
{
-
mySortedList[ i->second ] = i->first;
-
}
-
-
// print the sorted elements
-
cout << "sorted: ";
-
for ( map<double, string>::iterator i = mySortedList.begin();
-
i != mySortedList.end();
-
i++ )
-
{
-
cout << i->first << " {" << i->second << "} ";
-
}
-
cout << "\n";
-
-
}
-
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?
2 2776
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: -
map<int, string, Ascending> myMap;
-
-
bool Ascending(int first, int second)
-
{
-
return first < second;
-
}
-
-
OR:
-
-
map<int, string, Descending> myMap;
-
-
bool Descending(int first, int second)
-
{
-
return first > second;
-
}
-
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)
throw(std::runtime_error)
|
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...
|
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...
|
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>
|
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...
| |
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...
|
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...
|
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: 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. ...
|
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...
| |
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...
|
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...
|
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...
|
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...
|
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...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
| |
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
| |