473,586 Members | 2,555 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Map comparison between key value pairs

Hi,

How do I compare 2 maps for identical keys, and if they have identical
keys, check if they have identical values? In either case I want to
copy ONLY that key value pair to one of two different maps.

I know how to copy the entire map to another:
// Copy fset1 to fset3
std::copy(fset1 .begin(),fset1. end(),std::inse rter(fset3,
fset3.begin())) ;

And I can for example iterate through the entire map, but the key pair
comparison is beyond me at this point.

// Print test
for (std::map<doubl e,double>::cons t_iterator iter = fset3.begin();
iter != fset3.end(); ++iter )
{
std::cout << iter->first << " , " << iter->second << "\n";
}
Thank you for your time
Evyn

Jan 22 '07 #1
6 9853
Evyn wrote:
Hi,

How do I compare 2 maps for identical keys, and if they have identical
keys, check if they have identical values? In either case I want to
copy ONLY that key value pair to one of two different maps.

I know how to copy the entire map to another:
// Copy fset1 to fset3
std::copy(fset1 .begin(),fset1. end(),std::inse rter(fset3,
fset3.begin())) ;

And I can for example iterate through the entire map, but the key pair
comparison is beyond me at this point.

// Print test
for (std::map<doubl e,double>::cons t_iterator iter = fset3.begin();
iter != fset3.end(); ++iter )
{
std::cout << iter->first << " , " << iter->second << "\n";
}
Ponder:

#include <map>
#include <algorithm>
#include <iterator>
#include <iostream>

int main ( void ) {
std::map< int, int a;
std::map< int, int b;
b[2] = 1;
b[4] = 2;
b[3] = 1;
a[3] = 0;
a[5] = 2;
a[4] = 5;
a[2] = 3;

typedef std::map<int,in t>::const_itera tor const_map_iter;

const_map_iter a_iter = a.begin();
const_map_iter b_iter = b.begin();
while ( a_iter != a.end() && b_iter != b.end() ) {
if ( a_iter->first < b_iter->first ) {
++ a_iter;
continue;
}
if ( b_iter->first < a_iter->first ) {
++ b_iter;
continue;
}
// if we get here, keys match:
std::cout << "key : "
<< a_iter->first
<< " matches "
<< b_iter->first
<< ". value in a : "
<< a_iter->second
<< ". value in b : "
<< b_iter->second
<< '\n';
// do not forget to increment in this case:
++ a_iter;
++ b_iter;
}
}
Best

Kai-Uwe Bux
Jan 22 '07 #2
On Jan 22, 12:34 pm, "Evyn" <Evan.Dembs...@ gmail.comwrote:
Hi,

How do I compare 2 maps for identical keys, and if they have identical
keys, check if they have identical values? In either case I want to
copy ONLY that key value pair to one of two different maps.

I know how to copy the entire map to another:
// Copy fset1 to fset3
std::copy(fset1 .begin(),fset1. end(),std::inse rter(fset3,
fset3.begin())) ;

And I can for example iterate through the entire map, but the key pair
comparison is beyond me at this point.
You need to understand that an iterator to a map points to a std::pair,
so what's stored in the map is acctually std::pair-objects which
contains both the key and the value. The nice thing about this is that
you can compare std::pairs to each other, if you have two pairs, x and
y then
x == y
is the same thing as
x.first == y.first && x.second == y.second
which means that comparing the pairs in the tree compares both the keys
and the values.

I don't quite understand what you mean whit copying only to one of the
maps. Do you mean that you want to extract the key/value pairs that are
unique in the two maps? You might want to look at the
set_symmetric_d ifference or set_difference algorithms.

--
Erik Wikström

Jan 22 '07 #3
"Evyn" <Ev***********@ gmail.comwrote:
How do I compare 2 maps for identical keys, and if they have identical
keys, check if they have identical values? In either case I want to
copy ONLY that key value pair to one of two different maps.
So after the operation, the map should be identical, or do you want the
first map to have all the key/values it had plus any key/values that are
in the second map that the first didn't have?
I know how to copy the entire map to another:
// Copy fset1 to fset3
std::copy(fset1 .begin(),fset1. end(),std::inse rter(fset3,
fset3.begin())) ;
Is the end product of this operation what you want?
Jan 22 '07 #4
Essentially, I want to compare the key value pair in 2 maps and copy
all unique key value pairs to a third map. In the case where the keys
are not unique, I still want to copy the key value pair to the third
map, but where the value is higher...

Thanks for your time again

Jan 23 '07 #5

Evyn napsal:
Hi,

How do I compare 2 maps for identical keys, and if they have identical
keys, check if they have identical values? In either case I want to
copy ONLY that key value pair to one of two different maps.

I know how to copy the entire map to another:
// Copy fset1 to fset3
std::copy(fset1 .begin(),fset1. end(),std::inse rter(fset3,
fset3.begin())) ;

And I can for example iterate through the entire map, but the key pair
comparison is beyond me at this point.

// Print test
for (std::map<doubl e,double>::cons t_iterator iter = fset3.begin();
iter != fset3.end(); ++iter )
{
std::cout << iter->first << " , " << iter->second << "\n";
}
Thank you for your time
Evyn
See function Join:

#include <map>
#include <string>
#include <iostream>

typedef
std::map<std::s tring, intM;

void Dump(const std::string& prompt, const M& m)
{
std::cout << prompt << ":\n";
for (M::const_itera tor it = m.begin(); it != m.end(); ++it)
{
std::cout << '[' << it->first << "] = " << it->second << '\n';
}
std::cout << '\n';
}

template<typena me KEY, typename DATA>
void Join(
std::map<KEY, DATA>& result,
const std::map<KEY, DATA>& m1,
const std::map<KEY, DATA>& m2
)
{
result.insert(m 1.begin(), m1.end());
for (typename std::map<KEY, DATA>::const_it erator it = m2.begin();
it != m2.end();
++it
)
{
typename std::map<KEY, DATA>::iterator fi =
result.find(it->first);
if (fi == result.end())
result.insert(* it);
else
if (fi->second < it->second)
fi->second = it->second;
}
}

int main()
{
M m1;
M m2;

m1["a"] = 1;
m1["b"] = 2;
m1["c"] = 33;

m2["b"] = 22;
m2["c"] = 3;
m2["d"] = 44;

Dump("m1", m1);
Dump("m2", m2);

M result;
Join(result, m1, m2);

Dump("result", result);
};

Jan 23 '07 #6
On Jan 23, 1:32 pm, "Evyn" <Evan.Dembs...@ gmail.comwrote:
Essentially, I want to compare the key value pair in 2 maps and copy
all unique key value pairs to a third map. In the case where the keys
are not unique, I still want to copy the key value pair to the third
map, but where the value is higher...
std::set_union is what you want, together with an insert_iterator .

#include <algorithm>
#include <iostream>
#include <iterator>
#include <map>

bool cmp(
const std::pair<int, int>& p1,
const std::pair<int, int>& p2
)
{
if (p1.first < p2.first)
return true;
if (p1.first == p2.first)
return p1.second p2.second;
return false;
}

int main()
{
std::map<int, intm1;
std::map<int, intm2;
std::map<int, intmR;

// Set values
m1[1] = 1; m1[2] = 2; m1[3] = 3;
m1[4] = 4; m1[5] = 5; m1[6] = 6;

m2[1] = 3; m2[2] = 2; m2[4] = 5;
m2[5] = 3; m2[8] = 1; m2[9] = 4;

// Get insert iterator and run set_union
std::insert_ite rator<std::map< int, int ins(mR, mR.begin());
std::set_union( m1.begin(), m1.end(), m2.begin(), m2.end(), ins, cmp);

// Print
for (std::map<int, int>::iterator i = mR.begin(); i != mR.end(); ++i)
std::cout << i->first << " " << i->second << "\n";

return 0;
}

Hope the code survives the line-breaks.

--
Erik Wikström

Jan 23 '07 #7

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

Similar topics

6
4712
by: Equis Uno | last post by:
Hi, Assume I'm given a 100k file full of key-value pairs: date,value 26-Feb-04,36.47 25-Feb-04,36.43 24-Feb-04,36.30 23-Feb-04,37.00 20-Feb-04,37.00
9
1919
by: Xah Lee | last post by:
here's a interesting real-world algoritm to have fun with. attached below is the Perl documentation that i wrote for a function called "reduce", which is really the heart of a larger software. The implementation is really simple, but the key is to understand what the function should be. I'll post Perl and Python codes tomorrow for those...
1
3726
by: chris | last post by:
Does anyone have any tips for comparing large amounts of data? The data in question is around 1000 lines of up to 400 delimited key value pairs. Each line has a unique identifier stored in one of the fields. Each field needs to be compared to the corresponding line/field in a "master file". In addition, each field has formatting rules,...
2
15170
by: Dave | last post by:
Hello all, I would like to solicit suggestions on how to most efficiently accomplish something. I have profiled the project I'm working on and have found that calls to fabs() are taking a very substantial amount of time. fabs() is being used in the following manner: double a, b; a = get_a_value();
3
6292
by: He Shiming | last post by:
Hi Folks, Happy holidays! I have a question regarding STL multimap. Basically, the current multimap<int,int> look like this: key=>value 1=>10, 1=>20, 1=>30,
3
7583
by: Jeff L. | last post by:
I have an interesting problem and I'm not coming up with any answers in my searches, so hopefully someone can give me a hand with this. I have a feeling it's easy, but I usually get my nose stuck too far in the details to see the big picture. :) I want to create a collection of key/value pairs. I have a class for the key/value...
7
2491
by: Alan | last post by:
Hi. I have programmed in C++ before, but I`m a couple of years out of practice. I am seeking some advice on getting started on a quickie project. . . . I have to read a 54MB text file and do a pairwise comparison among 2500 items or so in the file. Each of those items have to be compared to every other item. Many of the comparison will...
2
2170
by: Sehboo | last post by:
I am trying to use key value pair list, but I don't want to use sorted list because it messes up my order. I am not sure what other options I have. Can anybody point? Thanks
0
7912
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, 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...
0
7839
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...
0
8338
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...
0
5390
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3837
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...
0
3865
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2345
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
1
1449
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
1180
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...

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.