According to the C++ Standard the iterator used in the call of the member function erase
of the class template std::vector shall be a valid dereferenceable const iterator.
However the iterator returned by the member function end
is not a dereferenceable iterator because it does not point to a valid element of the vector.
It seems you mean
#include <vector>
#include <iterator>
//...
v.erase( std::prev( v.end() ) );
Also you need to check whether the passed vector is empty and its size is equal to 0.
Pay attention to that your approach is inefficient.
You could write for example
double median( const std::vector<double> &v )
{
double value = 0.0;
if ( not v.empty() )
{
value = v[v.size() / 2];
if ( v.size() % 2 == 0 )
{
value = ( value + v[v.size() - 1] ) / 2;
}
}
return value;
}