Search code examples
c++sortingboostptr-vector

boost::ptr_vector sort function


i have a boost::ptr_vector that i want to keep maintained in sorted order.

i am creating a bunch of Customer objects containing several simple strings and values, which are then stored in a boost::ptr_vector, and they need to be ordered by one of the strings.

i have looked around but cant seem to find much information about the boost::ptr_vector member functions though, it appears that there is one called sort() which i think is relevant to what i am trying to do, however there is not much information on usage of this function.

is there an "insert in order" type function available to boost::ptr_vector? or do i need to load everything into the vector and then sort afterwards?

any help would be greatly appreciated thanks


Solution

  • You are asking too much of your ptr_vector. A ptr_vector is just a wrapper to std::vector. Likewise, there exist other wrappers to stdlib data structures.

    A data structure that is always sorted (by performing sorted inserts) is the std::set. To use that functionality, you can use a boost::ptr_set. See here: http://www.boost.org/doc/libs/1_54_0/libs/ptr_container/doc/ptr_set.html