This site uses cookies to analyze traffic and improve service.

By using this site, you agree to this use. See details.

Modern C++ idoms and recipes


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