There's nothing in the standard library that would do exactly what you want, you'll have to provide such a class yourself.
However, please note that it's a bad idea to publically inherit from a standard library container (such as std::map
); they are not designed for this, they don't have virtual functions and they don't have a virtual destructor. Consider this example to see why it's a bad idea:
template <class K, class V, class C, class A>
void foo(const std::map<K, V, C, A> &arg)
{
doSomething(arg.at(K()));
}
struct MyMap : std::map<int, int>
{
int at(int) { return 7; }
};
int main()
{
MyMap m;
foo(m); //this will call std::map::at, NOT MyMap::at
}
Instead, have your class store a std::map
(or perhaps std::unordered_map
, whichever is better for your implementation) by value. Or, if you think you could re-use a lot of the standard map's member functions and only override some, you could inherit from it non-publically and publish only the functions you need. Example:
template <
class Key,
class Value,
class Comparator = typename std::map<Key, Value>::key_compare,
class Allocator = typename std::map<Key, Value>::allocator_type
>
class DefaultDict : private std::map<Key, Value, Comparator, Allocator>
{
public:
// Publish the clear() function as is
using std::map<Key, Value, Comparator, Allocator>::clear;
// Provide my own at()
Value& at(const Key &key) {
return std::map<Key, Value, Comparator, Allocator>::operator[](key); //call the inherited function
}
// Etc.
};