How to get the first n elements of a std::map

c++, resize, shrink, stdmap

Solution

You can use `std::advance( iter, numberofsteps )` for that.

Problem

Since there is no .resize() member function in C++ std::map I was wondering, how one can get a std::map with at most n elements. The obvious solution is to create a loop from 0 to n and use the nth iterator as the first parameter for std::erase(). I was wondering if there is any solution that does not need the loop (at least not in my user code) and is more "the STL way to go".

Original source