473,472 Members | 2,139 Online
Bytes | Software Development & Data Engineering Community
Create 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 1449
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******@gascad.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
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...
4
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...
43
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...
3
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....
0
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 ...
6
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...
2
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...
3
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
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). ...
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
Oralloy
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,...
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...
1
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...
0
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...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
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 ...
0
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.