STL map sorting and merge?

C

cylin

Dear all,

If I declared a map<int,string> MapA;
How can I sort MapA by its data,i.e. string.
for example:

map<int,string> MapA;
MapA[1]=string("ccc");
MapA[2]=string("bbb");
MapA[3]=string("ddd");
MapA[4]=string("aaa");

I want to the result is (4,"aaa"),(2,"bbb"),(1,"ccc"),(3,"ddd");
Because key of a stl map is always ordered.
Is it possible to use sort(MapA.begin(),MapA.end(),?????) ?

Another is how to use merge() with 2 stl maps?

Thanks for your help.

Regards,
cylin.
 
J

John Harrison

cylin said:
Dear all,

If I declared a map<int,string> MapA;
How can I sort MapA by its data,i.e. string.
for example:

map<int,string> MapA;
MapA[1]=string("ccc");
MapA[2]=string("bbb");
MapA[3]=string("ddd");
MapA[4]=string("aaa");

I want to the result is (4,"aaa"),(2,"bbb"),(1,"ccc"),(3,"ddd");
Because key of a stl map is always ordered.
Is it possible to use sort(MapA.begin(),MapA.end(),?????) ?

Another is how to use merge() with 2 stl maps?

Thanks for your help.

None of these things are possible, you can transfer the map to another
container such as a vector and sort or merge it there.

It's not obvious from your code why you are using a map in the first place,
but I guess you have your reasons.

john
 
S

Siemel Naran

If I declared a map<int,string> MapA;
How can I sort MapA by its data,i.e. string.

No way. Standard answer is to have two maps. To avoid duplicating data you
can use pointers or smart pointers.
Is it possible to use sort(MapA.begin(),MapA.end(),?????) ?
No.

Another is how to use merge() with 2 stl maps?

I think std::set_union in #include <algorithm> does it. But you create a
third map (or array). There is no operator+= analog of merge, though maybe
if it works you could write one making use of the two arg insert function
where the function arg is a hint.
 
C

cylin

hmm.......
I think I don't understand value_comp() and value_compare.
I can't find any sample code to use them.

Thanks.
 
M

Michiel Salters

cylin said:
Dear all,

If I declared a map<int,string> MapA;
How can I sort MapA by its data,i.e. string.
for example:

map<int,string> MapA;
MapA[1]=string("ccc");
MapA[2]=string("bbb");
MapA[3]=string("ddd");
MapA[4]=string("aaa");

I want to the result is (4,"aaa"),(2,"bbb"),(1,"ccc"),(3,"ddd");
Because key of a stl map is always ordered.
Is it possible to use sort(MapA.begin(),MapA.end(),?????) ?

No, the fundamental property of a map is that it stores a collection
of pairs<Key,Value> sorted by their keys. It can never become sorted
inany other way. That is good, because that means that
MyFunc( map<int,string>& ) can assume its input is sorted, and the caller
konws it will be sorted after MyFunc returns.

You can of course use std::vector< std::pair<int, string> > with
suitable predicates. Write sort_by_first() and sort_by_second() functions,
they're trivial. You can then use std::sort( b, e, &sort_by_first ) on
your vector of pairs.
Another is how to use merge() with 2 stl maps?

That is a bit tricky, as you can't do a symmetrical merge. When you
have two values for one key, who wins?

Regards,
Michiel Salters
 
V

velthuijsen

cylin said:
Dear all,

If I declared a map<int,string> MapA;
How can I sort MapA by its data,i.e. string.
for example:

map<int,string> MapA;
MapA[1]=string("ccc");
MapA[2]=string("bbb");
MapA[3]=string("ddd");
MapA[4]=string("aaa");

I want to the result is (4,"aaa"),(2,"bbb"),(1,"ccc"),(3,"ddd");
Because key of a stl map is always ordered.
Is it possible to use sort(MapA.begin(),MapA.end(),?????) ?

Yes but you end up with a map that is sorted on the key.
If you want to have it sorted on the string make the string the key.
If you created it this way because the same string can appear more
often use multi_map
Another is how to use merge() with 2 stl maps?

Seeing the definition of merge() I'd say not. Rather use set_union
seeing that merge allows for duplicates and set_union doesnt. In
either case you have to provide your own functor seeing that you try
to compare two pairs.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top