tags:

views:

161

answers:

5

Whats the difference?

I want to be able to see if an element is in a HashMap and I just found out that if I do h[element], it will return the default element if it is not found, and not null. How would I use the iterator find method to see if the element is there?

Thanks

+2  A: 

You use the find method to see if something is in a std::map

std::map<std::string, std::string> myMap
std::map<std::string, std::string>::iterator it = myMap.find("foo");
if(it != myMap.end()) {
  //foo is in the map
} else {
    // foo isn't in the map
}

A const_iterator is an iterator that when de-referenced returns a const version of whatever it points to. In the example above, if it was a const_iterator then de-referencing it would yield a const std::string

Glen
+1  A: 

The main difference is that const_iterator cannot be used to modify the value of an element in the map.

using find method

 hash_map <int, int> hm1;
   hash_map <int, int> :: const_iterator hm1_RcIter = hm1.find( 2 );

   if ( hm1_RcIter == hm1.end( ) )
      cout << "The hash_map hm1 doesn't have an element "
           << "with a key of 2." << endl;
   else
      cout << "The element of hash_map hm1 with a key of 4 is: "
           << hm1_RcIter -> second << "." << endl;
aJ
+6  A: 

Assuming you're talking about STL and not some 3rd party library... m[key] doesn't just return the default object if key isn't in the map. It will create a new element in the map with that key and a default-constructed object as value.

You could use this:

map<string, int> mymap;
//add items to it
map<string, int>::iterator it = mymap.find("key");
if (it != myMap.end()) {
    // 'key' exists; (it->second) is the corresponding int
}

Or if you don't need to get the object (you just want to know if it exists):

map<string, int> mymap;
//add items to it
if (mymap.count("key") == 1) {
    // 'key' exists
}
Nicolás
Thanks for your correction, Pavel. I haven't used maps much :)
Nicolás
(I did know about ->second, I just didn't type it instinctively for not having used it much. I have no excuse for forgetting ::iterator though)
Nicolás
+1  A: 

As the other answers explain, for a std::map you can use find.

To answer the question in the headline:

For iterators, const can refer to the iterator itself, or the to the contents, the iterator points to. Both properties are orthogonal. With STL notation you have the following cases:

  • iterator Contents and iterator can be modified.
  • const_iterator Contents is const, the iterator can be modified
  • const iterator Contents can be modified, the iterator is const.
  • const const_iterator Contents and iterator are constant.

It is similar for pointers. There, the const can also refer to the contents or the pointer itself.

SebastianK
A: 

const iterators are need when you want an iterator to iterate through a const container. Trying to assign a non-const modifiable iterator onto a const container will return a compiler error. This is because the non-const iterator could potentially modify the const container.

doron