473,407 Members | 2,359 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

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

STL map sorting and merge?

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.
Jul 22 '05 #1
6 8739

"cylin" <cy***@avant.com.tw> wrote in message
news:c5************@ID-230325.news.uni-berlin.de...
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
Jul 22 '05 #2
"cylin" <cy***@avant.com.tw> wrote in message
news:c5************@ID-230325.news.uni-
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.
Jul 22 '05 #3
hmm.......
I think I don't understand value_comp() and value_compare.
I can't find any sample code to use them.

Thanks.
Jul 22 '05 #4
Thanks, I see.
Jul 22 '05 #5
"cylin" <cy***@avant.com.tw> wrote in message news:<c5************@ID-230325.news.uni-berlin.de>...
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
Jul 22 '05 #6
"cylin" <cy***@avant.com.tw> wrote in message news:<c5************@ID-230325.news.uni-berlin.de>...
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.
Jul 22 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Kakarot | last post by:
I'm gona be very honest here, I suck at programming, *especially* at C++. It's funny because I actually like the idea of programming ... normally what I like I'm atleast decent at. But C++ is a...
9
by: Hunter Hou | last post by:
Folks, I am just curious why standard library doesn't provide a "merge" operation without sorting, because sometimes I don't require sorting, just merge. I looked at list.merge() and merge()...
8
by: Mike MacSween | last post by:
tblCourses one to many to tblEvents. A course may have an intro workshop (a type of event), a mid course workshop, a final exam. Or any combination. Or something different in the future. At...
5
by: mhk | last post by:
Hi , is there any way to merge three sorted arrays into a sorted file, without using 4th array. i guess 3 way merge sort is the only option but i dont know its algorithem. can anyone tell me...
27
by: ruel loehr | last post by:
Hey guys, I am looking for some insight: Given two sorted arrays of integers A and B, where array B has enough extra room in it to hold the contents of both A and B. Merge array A and B...
4
by: Graham | last post by:
Can someone point me to an algorithm for sorting double precision values that is stable with duplicates? Thanks, Graham
1
by: PhilB | last post by:
I have been having issues trying to merge sort a double linked list. (I would supply the code, but it is locked away on an inaccessable computer.) The list always results in an unsorted list, but...
20
by: martin-g | last post by:
Hi. Mostly I program in C++, and I'm not fluent in C# and .NET. In my last project I began to use LinkedList<and suddenly noticed that can't find a way to sort it. Does it mean I must implement...
12
by: bisuvious | last post by:
hi all, I am looking for a technique to sort data of a large file, say 2GB/ 4GB. Please suggest any algorithm, if you have any idea. thanks bisuvious
4
by: slapsh0t11 | last post by:
Hello! I need help with a program that I believe I am nearly done with. However, there seems to be a few details that preclude me from success. Here is my assignment: Here is my class file...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
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,...
0
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...
0
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...
0
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...
0
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,...
0
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...

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.