Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++ key-value container with variable type values

Tags:

c++

algorithm

What I need is a key-value container for keeping some well-known parameters of objects. All possible keys are known on compile time. Values are belong to differnt types: POD (integers, pointers) and non-POD (some small structures with contructors).

Current implementation uses very big structure and tons of code to initialize, fill and copy values. So I want to replace this structure with container. Container must provide: 1) quick access by key (constant time). 2) the possibility to iterate over all values to copy them.

I tried to think up some array based approach, but coldn't make it. I can make some hash table, but I don't know what to do with different value types.

like image 643
pashkoff Avatar asked Sep 15 '25 13:09

pashkoff


2 Answers

sounds like std::unordered_map (or boost::unordered_map) is the right solution. Just use boost::any for the objects so they can be any type.

like image 92
Evan Teran Avatar answered Sep 18 '25 08:09

Evan Teran


You might want to look at Boost.Variant for storing the values. Then you can just use a std::map<Key, boost::variant> or std::unordered_map<Key, boost::variant> for your container.

like image 27
Sean Avatar answered Sep 18 '25 09:09

Sean