473,734 Members | 2,511 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Handling maps.

Hi all,

Say I have two maps m1 and m2. Both have key as std::string and value
as long.

m1 could hold entries like
"abc" 123
"def" 456

m2 could have entries like
"abc" 789
"hij" 111

I want to show the union of these results like this

"abc" 123 789
"def" 456 0
"hij" 0 111

One way to deal with it would be to form a map<string, vector<long> >,
then loop over m1 and fill it, then loop over m2 and if there is some
existent entry for a key then push_back the value in vector. Doesn't
seem very clean to me. I am sure there might be better approaches. Can
you suggest some ?

Should I have something like map<string, long[2]> instead of m1 and m2
in the first place ?

Thanks.
Jul 22 '05 #1
2 1464
Old Monk wrote:

Hi all,

Say I have two maps m1 and m2. Both have key as std::string and value
as long.

m1 could hold entries like
"abc" 123
"def" 456

m2 could have entries like
"abc" 789
"hij" 111

I want to show the union of these results like this

"abc" 123 789
"def" 456 0
"hij" 0 111

One way to deal with it would be to form a map<string, vector<long> >,
then loop over m1 and fill it, then loop over m2 and if there is some
existent entry for a key then push_back the value in vector. Doesn't
seem very clean to me.
It's probably the way I would do it (the lazy way)
I am sure there might be better approaches. Can
you suggest some ?
The idea of adapting the idea behind merge sort comes to my mind.

initialize top_first with the first entry in map1
inttialize top_scnd with the first entry in map2

while top_first && top_secnd {

if *top_first == *top_secnd
build new entry combining *top_first and *top_scnd, add to map3
advance top_first
advance top_secnd

else
if *top_first < *top_scnd
build new entry from *top_first, add to map3
advance top_first
else
build new entry from *top_secnd, add to map3
advance top_secnd
}
// while loop finished, either top_first or top_secnd
// or both have reached the end of their map. But one of
// the maps may still contain eintries, add them to the
// result
while top_first {
build new entry from *top_first, add to map3
advance top_first
}

while top_secnd {
build new entry from *top_secnd, add to map3
advance top_secnd
}
Something like that. Might be faster since no search through
the maps is involved.


Should I have something like map<string, long[2]> instead of m1 and m2
in the first place ?


long[2] *would* be fine. But since plain vanilla arrays don't satisfy the
copy and assignment requirements imposed by the standard containers, it
cannot be used. But you could do:

struct Entry
{
long Value1;
long Value2;
}

map< string, Entry >

If that is ok for m1 and m2 is only you to decide. I wouldn't do it. The
operation you want to do is not a standard one, so why blow up the input
maps needlessly. For the result you can either use

map< string, vector< long > >

or
map< string, Entry >

it doesn't make much of a difference (in this specific case. If you
sometimes plan to merge 3 input maps, then things change. But for the
moment ...)

--
Karl Heinz Buchegger
kb******@gascad .at
Jul 22 '05 #2
In article <41************ ***@gascad.at>,
Karl Heinz Buchegger <kb******@gasca d.at> wrote:
Old Monk wrote:

Hi all,

Say I have two maps m1 and m2. Both have key as std::string and value
as long.

m1 could hold entries like
"abc" 123
"def" 456

m2 could have entries like
"abc" 789
"hij" 111

I want to show the union of these results like this

"abc" 123 789
"def" 456 0
"hij" 0 111

One way to deal with it would be to form a map<string, vector<long> >,
then loop over m1 and fill it, then loop over m2 and if there is some
existent entry for a key then push_back the value in vector. Doesn't
seem very clean to me.


It's probably the way I would do it (the lazy way)
I am sure there might be better approaches. Can
you suggest some ?


The idea of adapting the idea behind merge sort comes to my mind.

initialize top_first with the first entry in map1
inttialize top_scnd with the first entry in map2

while top_first && top_secnd {

if *top_first == *top_secnd
build new entry combining *top_first and *top_scnd, add to map3
advance top_first
advance top_secnd

else
if *top_first < *top_scnd
build new entry from *top_first, add to map3
advance top_first
else
build new entry from *top_secnd, add to map3
advance top_secnd
}
// while loop finished, either top_first or top_secnd
// or both have reached the end of their map. But one of
// the maps may still contain eintries, add them to the
// result
while top_first {
build new entry from *top_first, add to map3
advance top_first
}

while top_secnd {
build new entry from *top_secnd, add to map3
advance top_secnd
}
Something like that. Might be faster since no search through
the maps is involved.


Should I have something like map<string, long[2]> instead of m1 and m2
in the first place ?


long[2] *would* be fine. But since plain vanilla arrays don't satisfy the
copy and assignment requirements imposed by the standard containers, it
cannot be used. But you could do:

struct Entry
{
long Value1;
long Value2;
}

map< string, Entry >

If that is ok for m1 and m2 is only you to decide. I wouldn't do it. The
operation you want to do is not a standard one, so why blow up the input
maps needlessly. For the result you can either use

map< string, vector< long > >

or
map< string, Entry >

it doesn't make much of a difference (in this specific case. If you
sometimes plan to merge 3 input maps, then things change. But for the
moment ...)


Another option would be to use std::pair, as in:

map< string, pair< long, long > >;
Jul 22 '05 #3

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

Similar topics

4
2060
by: Jeff Sandys | last post by:
I'm trying to write a mapping function for genealogy. I want to read a gedcom database and plot an icon at a geographic location based on a user's query. Can you help me find: 1) A python interface to gedcom? (gedcom is a well documented linear file so I can probably do this myself if an interface is not available)
4
2215
by: Stanimir Stamenkov | last post by:
I have this kind of construct: http://www.geocities.com/stanio/temp/usemap01.html (an IMG element using MAP described with AREA elements) I'm trying to make it more accessible and currently I'm testing in Lynx. Lynx plays it clever and when I "enter" the map it shows only 3 links no matter there are 5 AREAs (3 point to a same reference). I don't like Lynx displays the 'alt' text and not the link titles
43
5008
by: Steven T. Hatton | last post by:
Now that I have a better grasp of the scope and capabilities of the C++ Standard Library, I understand that products such as Qt actually provide much of the same functionality through their own libraries. I'm not sure if that's a good thing or not. AFAIK, most of Qt is compatable with the Standard Library. That is, QLT can interoperate with STL, and you can convert back and forth between std::string and Qt::QString, etc. Are there any...
3
61091
by: Sean | last post by:
Have you ever wanted to add the great features inherent in Google Maps? Here is how you do it. ============== == STEP ONE == ============== Create a new MS Access form called frmGoogleMap. Size the form to your liking...
0
1405
by: Philadelphia XML User Group | last post by:
NEXT MEETING: March 8th, 6:00 to 8:00 pm A Definitive Introduction to Topic Maps with Michel Biezunski and Roger Sperberg To sign up, please visit http://www.xmlphilly.org/signup.asp There's been lots of buzz about Topic Maps in the XML community, and
6
2987
by: Sam Carleton | last post by:
Ok, over the years I have read about doing web programing and I have done some real basic stuff. Now I am digging into some real ASP.Net 2.0 and am totally lost some things. I have a master page setup and that is working great. On my contact page I would like to use Google Maps API Version 2 to show a map to my location. Below is the first Google example. I would like to add this to my aspx page that is using the master page. I...
2
1769
by: rn5arn5a | last post by:
I am not sure where I should have posted this question in this newsgroup. Please excuse me if I am wrong. Nowadays, a lot of websites have come with Maps (Google Maps being an example). Can someone please tell me whether these maps are created by using graphic/imaging softwares (like Adobe PhotoShop)? Actually one of my clients who is into real estate wants such maps incorporated in his website & asked me whether such maps can be...
3
4437
by: Phil Stanton | last post by:
I have a button on a form which when pressed displays a google map of the address. Code is Private Sub Googlemap_Click() MakeURL ("") End Sub
6
3112
by: Time Waster | last post by:
Java property files are dead simple: key1=val1 some.key2=val2 For simplicity on the Java side, I'd like to use these files from C as well (the C program and Java program must cooperate). Anyone have any bright ideas on handling this sort of thing from C? Is there any preexisting libraries or calls that would help? One idea I had was to have a big enumerated type of the key names (which are
0
8776
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,...
0
9310
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9182
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 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...
0
8186
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, 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...
1
6735
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4550
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 the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4809
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3261
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
3
2180
bsmnconsultancy
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...

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.