Your best option is to use the at()
method, which is const
and will throw an exception if the key is not found.
int A::getValue(int key) const
{
return m.at(key);
}
Otherwise, you would have to decide what to return in the case where the key is not found. If there is a value you can return in these cases, then you can use std::map::find
:
int A::getValue(int key) const
{
auto it = m.find(key);
return (it != m.end()) ? it->second : TheNotFoundValue;
}