Function vector_sort (o2scl)

O2scl : Function List

template<class vec_t, class data_t>
void o2scl::vector_sort(size_t n, vec_t &data)

Sort a vector (in increasing order)

This is a generic sorting template function using a heapsort algorithm. It will work for any types data_t and vec_t for which

  • data_t has a non-const version of operator=

  • data_t has a less than operator to compare elements

  • vec_t::operator[] returns a non-const reference to an object of type data_t

In particular, it will work with the STL template class std::vector, and arrays and pointers of numeric, character, and string objects.

For example,

std::string list[3]={"dog","cat","fox"};
vector_sort<std::string[3],std::string>(3,list);

This works similarly to the GSL function gsl_sort_vector().

Note

With this function template alone, the user cannot avoid explicitly specifying the template types for this function because there is no parameter of type data_t, and function templates cannot handle default template types. For this reason, the function template o2scl::vector_sort_double() was also created which provides the convenience of not requiring the user to specify the vector template type.

Note

This sorting routine is not stable, i.e. equal elements have arbtrary final ordering

Note

If n is zero, this function will do nothing and will not call the error handler.