Is there a sorted_vector class, which supports insert() etc.?

后端 未结 6 867
情话喂你
情话喂你 2020-12-24 04:34

Often, it is more efficient to use a sorted std::vector instead of a std::set. Does anyone know a library class sorted_vector, which b

6条回答
  •  野趣味
    野趣味 (楼主)
    2020-12-24 05:20

    Boost.Container flat_set

    Boost.Container flat_[multi]map/set containers are ordered-vector based associative containers based on Austern's and Alexandrescu's guidelines. These ordered vector containers have also benefited recently with the addition of move semantics to C++, speeding up insertion and erasure times considerably. Flat associative containers have the following attributes:

    • Faster lookup than standard associative containers
    • Much faster iteration than standard associative containers.
    • Less memory consumption for small objects (and for big objects if shrink_to_fit is used)
    • Improved cache performance (data is stored in contiguous memory)
    • Non-stable iterators (iterators are invalidated when inserting and erasing elements)
    • Non-copyable and non-movable values types can't be stored
    • Weaker exception safety than standard associative containers (copy/move constructors can throw when shifting values in erasures and insertions)
    • Slower insertion and erasure than standard associative containers (specially for non-movable types)

    Live demo:

    #include 
    #include 
    #include 
    
    using namespace std;
    
    int main()
    {
        boost::container::flat_set s;
        s.insert(1);
        s.insert(2);
        s.insert(3);
        cout << (s.find(1)!=s.end()) << endl;
        cout << (s.find(4)!=s.end()) << endl;
    }
    

    jalf: If you want a sorted vector, it is likely better to insert all the elements, and then call std::sort() once, after the insertions.

    boost::flat_set can do that automatically:

    template 
    flat_set(InputIterator first, InputIterator last, 
             const Compare & comp = Compare(), 
             const allocator_type & a = allocator_type());
    

    Effects: Constructs an empty set using the specified comparison object and allocator, and inserts elements from the range [first, last).

    Complexity: Linear in N if the range [first, last) is already sorted using comp and otherwise N*log(N), where N is last - first.

提交回复
热议问题