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,"b bb"),(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. 6 8766
"cylin" <cy***@avant.co m.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,"b bb"),(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
"cylin" <cy***@avant.co m.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.
hmm.......
I think I don't understand value_comp() and value_compare.
I can't find any sample code to use them.
Thanks.
"cylin" <cy***@avant.co m.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,"b bb"),(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_fir st ) 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
"cylin" <cy***@avant.co m.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,"b bb"),(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. This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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 different
story.
Linked lists have been giving me a headache. I can barely manage to
create a single/doubly linked list, it's just that when it gets to
merge sorting or searching (by reading shit data from text files), I
lose track.
|
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() algorithm, both require sorted
sequenece.
Thanks,
Hunter
|
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 the moment the printed output is usually going to Word. It's turning into
an unholy mess, because I'm having to prepare umpteen different Word
templates, and the queries that drive them, depending on what events a
course has.
|
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 algorithm of 3 way merge sort or anyother way to
solve this problem.
Thank you veryyyyy much.
|
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 together such that the result is the sorted combination of the
two (do not remove duplicates) and the result resides in the B array.
I cannot perform any memory allocations to solve the problem.
| |
by: Graham |
last post by:
Can someone point me to an algorithm for sorting double precision
values that is stable with duplicates?
Thanks,
Graham
|
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 no errors. I think I have tracked it down to the recursive functions locking on a node and then the node being moved during the sorting process.
I used an example which sorts an array, so I had to make some guesses on interfacing it with a...
|
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 sorting for
LinkedList<myself?
Thanks in advance
Martin
|
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
|
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 (Sorts.java):
import java.util.*;
/**
|
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
| |
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
|
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.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
|
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 most users, this new feature is actually very convenient. If you want to control the update process,...
|
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
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 launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
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
|
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |