2

I hav two stl maps like map<int,int> and i want to compare them.. so here is the code..

map <int, int> a,b;
insert into a and b;
map<int,int>::iterator i;

for(i=a.begin();i!=a.end();i++){
    if(what should be here?)
         then cout << (*i).first << " also present in b" << endl;
}

I was hoping that something like (b[(*i).first]).exist??

Prasanth Madhavan
  • 12,657
  • 15
  • 62
  • 94
  • possible duplicate of [How to check if std::map contains a key without doing insert?](http://stackoverflow.com/questions/3886593/how-to-check-if-stdmap-contains-a-key-without-doing-insert) – Keith Pinson Apr 18 '14 at 21:35
  • @Kazark : after 3 years is that still relevant? and i guess my question has better clarity. – Prasanth Madhavan Apr 19 '14 at 08:48

8 Answers8

7

Use map::find as:

for(i=a.begin(); i!=a.end(); i++)
{
  if( b.find(i->first) != b.end() )
       std::cout << (*i).first << " also present in b" << std::endl;
}

Note i->first and (*i).first are same.

Nawaz
  • 353,942
  • 115
  • 666
  • 851
3

Use map::find().

Oliver Charlesworth
  • 267,707
  • 33
  • 569
  • 680
3

This will return true if i->first (the key of the element in a pointed by i) is present in b.

if (b.find(i->first) != b.end())
Victor
  • 348
  • 2
  • 12
2

You can't use operator[], because that will create the key if it doesn't exist. Instead, use find:

map<int,int>::const_iterator it = b.find(a->first);
if( it == b.end() )
  // NOT FOUND
John Dibling
  • 99,718
  • 31
  • 186
  • 324
1

std::map has a function named find for finding a key in the map. See this

So the code should be :

if( b.find( i->first ) != b.end() )
Christopher
  • 8,912
  • 3
  • 33
  • 38
0

I like b.count(i->first) > 0.

SCFrench
  • 8,244
  • 2
  • 31
  • 61
  • -1: I don't because that will go through the whole map. If its a multimap or a multiset, this is wasteful. You only need to know that there is at least one; no need to count them all up. – John Dibling Jun 03 '11 at 13:57
  • What?? A) it's not a multimap, so B) no it won't go through the whole map. Sure, if it was a multimap I'd use find, but this is more concise and works fine for maps. – SCFrench Jun 03 '11 at 13:57
  • See for example http://stackoverflow.com/questions/3886593/check-if-stdmap-contains-a-key-without-doing-insert/3886599#3886599 (which, by the way, this question is probably a dup of). – SCFrench Jun 03 '11 at 14:13
  • @John: Even in a multimap, it won't go through the whole map; at worst, it will iterate through the key's range. – Mike Seymour Jun 03 '11 at 14:53
0

map::find function

I will do as this :

std::map <int, int> a,b;
insert into a and b;
std::map<int,int>::iterator it = a.begin();
std::map<int,int>::iterator ite = a.end();

while (it != ite)
{
  if (b.find(it->first) != b.end())
  {
    std::cout << it->first << " also present in b" << std::endl;
  }
  ++it;
}
Errata
  • 640
  • 6
  • 10
0

For your task for performance reasons I would suggest something like:

map<int,int>::iterator ia  = a.begin(), ib  = b.begin(),
                       iae = a.end()  , ibe = b.end();
while(ia != iae && ib != ibe) {
  if      (ia->first < ib->first) ia++;
  else if (ia->first > ib->first) ib++;
  else {
    cout << ia->first << " also present in b" << endl;
    ia++; ib++;
  }

}
Oleg Svechkarenko
  • 2,508
  • 25
  • 30