tags:

views:

129

answers:

2

std::map find/end both provides const_iterator and iterator, e.g.

  iterator end ();
  const_iterator end () const

Out of curiosity,if I have a std::map , which will be called/compared here, an iterator or a const_iterator ? :

if(m.find(key) != m.end()) {
   ...
}

And should I care ?

+8  A: 

If m is const, then a const_iterator will be returned; otherwise an iterator will be returned.

If all you are doing is testing for existence of an element in the map, then it doesn't really matter which one is used.

James McNellis
It matters when you write you own algorithm. Then you should specify whether your algorithm is a modifying algorithm. It if isn't then you're saying it works with `const_iterator`. It's also a good idea to know which algorithms you call are modifying and which aren't: this way when you get an error you can tell it's because you passed a `const_iteratr` (perhaps because it comes from a `const` object) to a modifying algorithm.
wilhelmtell
A: 

It depends on if your map is const or not. If it is, you'll get a const_iterator. If not, you get an iterator.

Fred Larson