Trying to write a method that removes the first (with the least keys) N elements from std :: map. Tried this:
void EraseNMapElements(const int numElementsToRemove){ const int originalSize = _map.size(); auto eraseIter = _map.begin(); std::advance(eraseIter, numElementsToRemove); _map.erase(_map.begin(), eraseIter); assert(_map.size() == (originalSize - numElementsToRemove)) || (0 == originalSize) || (0 == _map.size())); }
It works when the number of elements is greater than the number that you want to delete. Therefore, if I had five elements, ask to delete 2, the last element will remain. However, if I have one element and a delete request of 2, I have one element left.
Is there a neat way to cover this? I could force the IF statement around checking for numElementsToRemove more than map.size (), but should there be a better solution?
c ++ c ++ 11 stdmap
user997112
source share