I often run into this problem: given the sequence, find the k-smallest element. The question is not so complicated, but what I'm looking for is an โidiomaticโ way to do this, which is safe (there is little room for error) and well communicate intentions. So the final result sorts the sequence and then takes the first element k:
std::sort(container.begin(),container.end()); std::vector<T> k_smallest(container.begin(),container.begin() + k);
It seems safe and understandable to me, but the complexity here is nlogn + k, not just n. As you guys do this, there is an idomatic way (perhaps using some kind of obscure function) that would provide optimal complexity without having to reuse the wheel
source share