Guest User

Untitled

a guest
Jul 19th, 2018
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.33 KB | None | 0 0
  1. template <class T>
  2. T median (std::vector<T> v) {
  3. typedef typename vector<T>::size_type vec_sz;
  4. vec_sz size= v.size();
  5. if (size == 0)
  6. throw domain_error("median of an empty vector");
  7. sort(v.begin(), v.end());
  8. vec_sz mid = size / 2;
  9. return size % 2 == 0 ? (v[mid] + v[mid - 1]) / 2 : v[mid];
  10. }
Add Comment
Please, Sign In to add comment