C++计算中值

Talk is cheap. Show me the code.

1
2
3
4
5
6
7
8
9
10
#include <vector>
#include <algorithm>

template<typename T>
T GetMedianValue(std::vector<T>& vec)
{
std::nth_element(vec.begin(), vec.begin() + vec.size() / 2, vec.end());

return vec[vec.size() / 2];
}