ARTICLE AD BOX
I have an insertion-ordered Map, and implement it as (1) std::map with the actual keys/values + (2) an additional separate vector storing keys as they are inserted.
std::map<std::string, MyObject* obj> map; std::vector<std::string> orderVector;There is a utility function to delete any given key from a map, which will remove from both (1) the map and (2) the order vector:
void removeEntry(const std::string& key) { auto iter = map.find(key); if (iter != map.end()) { value = iter->second; // ... Perform additional logic, e.g. "delete value" // (1) Remove from main map map.erase(key); // (2) Remove from order vector std::erase_if(orderVector, [key](T t) { return t == key; }); } }I also need a removeAllEntries() function to remove everything. Removal must happen in the exact reverse order, which is why I have an ordered map. Unfortunately I can't call my single function in a loop,
for (auto it = orderVector.rbegin(); it != orderVector.rend(); ++it) { // Reverse removeEntry(orderVector[i]); }because this sub-function also removes from the orderVector, which it must in its single form.
I don't want to rewrite the single removeEntry function to return an iterator because it must exist in its current form, as a plain void. Both functions must be available with simple signatures and ideally one should be reused from the other. Are there any good solutions to this?
I suppose I can have a separate removeEntryInMapOnly() that only does the Map removal, which is loop-safe, and then after that do a orderVector.clear() to clear out the Order Vector, but it doesn't seem very clean or elegant to me.
