开发者

Dividing a set (C++)

开发者 https://www.devze.com 2023-01-23 20:45 出处:网络
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 f

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/

0

精彩评论

暂无评论...
验证码 换一张
取 消