I have a table that is accessed by row and column, where those two are not integral. They will, however be unique and taken from the same set. The table will need to be expanded, but always from the end. Removal may be necessary from the middle, but is not a priority.
I am currently testing 2 approaches:
map<Key, int> headers;
vector<vector<Value> > table;
Or:
map<Key, map<Key, Value> > table;
Which is going to be more appropriate? I am also open to new suggestions.
Examples showing basic usage (though both very much oversimplified) are here and here.
It all depends on how this structure is going to be used: How densely populated the table will be, how efficient various operations have to be, how big the payload type (
Value) will be, etc.Your first approach (vector of vectors, with a map to translate indices) is a dense representation: Every value is stored explicitly in the table. If vectors grow by a factor of L, then the total allocation excess for the data proper can go up to L^2. For example, if L == 1.25, you may end up over 50% excess storage; if
sizeof(Value)is large or if your table is large, that may be prohibitive. Also expanding the table may occasionally be quite expensive (when the vectors must be reallocated).Your second approach (map of maps) is potentially sparse. However, if all table (row, column) pairs are accessed, it will get dense. Also, the book-keeping information for maps is a bit larger than for vectors. So for small
Valuesizes, the vector of vectors approach might be more space-efficient. If most of your table will be populated by “default” values, then you might improve things by distinguishing between read and write access to the table: A read for a value could perform a “find” and return a synthesized default value if notable entry was found.