I've got a std::set<int>
which has n
items in it. And I want to get rid of n-k
bigger elements and keep the firs开发者_运维技巧t (least) k
elements. How should I do so? Is there a pre-defined function for this?
A std::set
is ordered.
std::set<int>::const_iterator i = myset.begin();
std::advance(i, k);
myset.erase(i, myset.end());
Use the erase function :
http://www.cplusplus.com/reference/stl/set/erase/
精彩评论