That would be:
Code:
class someclass {
public:
element& get_element(const std::string& key) { return m_map[key]; }
private:
std::map<std::string, element> m_map;
};
Of course, if key does not exist then it would be added to the map. If you do not want this behaviour, then another option is:
Code:
class someclass {
public:
element& get_element(const std::string& key)
{
std::map<std::string, element>:iterator iter = m_map.find(key);
if (iter == m_map.end())
{
throw std::out_of_range("Invalid key: " + key);
}
return *iter;
}
private:
std::map<std::string, element> m_map;
};
Yet another option is to stick to what you have now, but state that if someone attempts to use a key that does not exist, the behaviour is undefined.