Associative container - exception rather than default-construct in C++
I'm looking for an associative container in C++, where rather than requiring it's elements to be default-constructible, will throw an exception where a key is requested that isn't found - edit: in the appropriate functions, obviously not all of them require default-construction. This is basically because I'm replacing the Standard allocator with one that phones home and tells the master object how much memory is being allocated, and hence开发者_高级运维 needs to know where "home" is.
The simplest answer for an std::map, is not using operator[]
. Use insert
to create the new elements and find
to locate an existing element. If you do not use operator[]
the contained type does not need to be default constructible, and you can decide to throw and exception if find
does not locate the object.
Are you looking for the at
members of boost::unordered_map
? These are not in std::tr1::unordered_map
, AFAICT.
精彩评论