Modern C++ idoms and recipes

meshell
59.2K views

Open Source Your Knowledge, Become a Contributor

Technology knowledge has to be shared and made accessible for free. Join the movement.

Create Content

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);
};
Open Source Your Knowledge: become a Contributor and help others learn. Create New Content