1

What data structure do you prefer if you have two keys, and can not use boost::multiindex?

I can have multiple records with Key1 and/or Key2 but there will be only one record for the combination of KEY1 KEY2. My requirements are that I should be able to search

  1. All the records for KEY1
  2. All the records for KEY2
  3. Single record given KEY1 and KEY2

Currently I am using std::map

std::map <CString, std::vector<CutomClass>> m_map;

the So first key is used in map and another is part of my class (this class has more data alone with second key)

Is there any other data structure I can use instead?

I cant use boost library for some reason so I am looking for suggestion only from standard library.

Cyrille Ka
  • 15,328
  • 5
  • 38
  • 58
Geek
  • 273
  • 5
  • 19

1 Answers1

1

The way I would implement this would be with three data structures. Two multi_map (or unordered_multimap), one keyed off KEY1, and another keyed of KEY2.

The third would me map (unordered_map) of KEY1 + KEY2 combination. For map, that would be simple and straightforward, for unordered_map, you'd have to combine the key, or, hash two subkeys and than combine the hash.

And I seem to forget to mention, only one of those maps (keyed of combined key) will hold actual values, two other maps will hold pointers to the values held in combined map.

SergeyA
  • 61,605
  • 5
  • 78
  • 137