Fastest way of finding the middle value of a triple?

前端 未结 25 970
庸人自扰
庸人自扰 2020-12-04 12:20

Given is an array of three numeric values and I\'d like to know the middle value of the three.

The question is, what is the fastest way of finding the midd

25条回答
  •  刺人心
    刺人心 (楼主)
    2020-12-04 12:52

    This one will work:

    template T median3_1_gt_2(const T& t1, const T& t2, const T& t3) {
        if (t3>t1) {
            return t1;
        } else {
            return std::max(t2, t3);
        }
    }
    template T median3(const T& t1, const T& t2, const T& t3) {
        if (t1>t2) {
            return median3_1_gt_2(t1, t2, t3);
        } else {
            return median3_1_gt_2(t2, t1, t3);
        }
    }
    

    https://github.com/itroot/firing-ground/blob/864e26cdfced8394f8941c8c9d97043da8f998b4/source/median3/main.cpp

提交回复
热议问题