views:

2094

answers:

4

Is there a reason why passing a reference to a STL map as const causes the [] operator to break? I get this compiler error (gcc 4.2) when I use const:

error: no match for ‘operator[]’ in ‘map[name]’

Here's the function prototype:

void func(const char ch, std::string &str, const std::map<std::string, std::string> &map);

And, I should mention that there is no problem when I remove the const keyword in front of std::map.

If I've been instructed correctly, the [] operator will actually insert a new pair into the map if it doesn't find the key, which would of course explain why this happens, but I can't imagine that this would ever be acceptable behavior.

If there is a better method, like using find instead of [], I'd appreciate it. I can't seem to get find to work either though... I receive const mismatched iterator errors.

Thanks.

+5  A: 

When you're using operator[], std::map looks for item with given key. If it does not find any, it creates it. Hence the problem with const.

Use find method and you'll be fine.

Can you please post code on how you're trying to use find() ? The right way would be :

if( map.find(name) != map.end() )
{
   //...
}
Benoît
Got it, thanks :)
Joe
+2  A: 

Probably because there is no const operator[] in std::map. operator[] will add the element you're looking for if it doesn't find it. Therefore, use the find() method if you want to search without the possibility of adding.

Fred Larson
+8  A: 

Yes you can't use operator[]. Use find, but note it returns const_iterator instead of iterator:

std::map<std::string, std::string>::const_iterator it;
it = map.find(name);
if(it != map.end()) {
    std::string const& data = it->second;
    // ...
}

It's like with pointers. You can't assign int const* to int*. Likewise, you can't assign const_iterator to iterator.

Johannes Schaub - litb
Thank you all very much :)
Joe
+1  A: 

For your "const mismatched iterator errors" :

find() has two overloads:

      iterator find ( const key_type& x );
const_iterator find ( const key_type& x ) const;

My guess is that you're getting this error because you're doing something like assigning a non-const iterator (on the left) to the result of a find() call on a const map:

iterator<...> myIter /* non-const */ = myConstMap.find(...)

That would result in an error, though perhaps not the one you're seeing.

Dan Breslau
Yes, that was it. Thanks :)
Joe