Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can anyone recommend a C++ std::map replacement container?

Tags:

c++

stdmap

Maps are great to get things done easily, but they are memory hogs and suffer from caching issues. And when you have a map in a critical loop that can be bad.

So I was wondering if anyone can recommend another container that has the same API but uses lets say a vector or hash implementation instead of a tree implementation. My goal here is to swap the containers and not have to rewrite all the user code that relies on the map.

Update: performance wise the best solution would be a tested map facade on a std::vector

like image 901
Robert Gould Avatar asked Sep 24 '08 07:09

Robert Gould


1 Answers

You can use std::tr1::unordered_map, which is already present in most STL implementations, and is part of the C++0x standard.

Here is it's current signature :

template <class Key,
          class T,
          class Hash = std::tr1::hash<Key>,
          class Pred = std::equal_to<Key>,
          class Alloc = std::allocator<std::pair<const Key, T> > >
class unordered_map;
like image 152
Luc Touraille Avatar answered Dec 26 '22 16:12

Luc Touraille