Back
Close

Modern C++ idoms and recipes

meshell
57.6K views

Keep std::vector sorted

Possible solution

template<typename Cont, typename T>
void insert_sorted(Cont& container, const T& item) {
    const auto insert_pos = std::lower_bound(std::begin(container), std::end(container),item);
    container.insert(insert_pos, item);
};
Create your playground on Tech.io
This playground was created on Tech.io, our hands-on, knowledge-sharing platform for developers.
Go to tech.io