Operator< and strict weak ordering

前端 未结 6 1975
醉酒成梦
醉酒成梦 2020-11-22 03:51

How to define operator< on n-tuple (for example on 3-tuple) so that it satisfy strict weak ordering concept ? I know that boost library has

6条回答
  •  谎友^
    谎友^ (楼主)
    2020-11-22 04:06

    The basic flow should be along the lines of: if the Kth elements are different, return which is smaller else go to next element. The code following assumes you don't have a boost tuple otherwise you would use get(tuple) and not have the problem to begin with.

    if (lhs.first != rhs.first)
        return lhs.first < rhs.first;                
    if (lhs.second != rhs.second)
        return lhs.second< rhs.second;
    return lhs.third < rhs.third;
    

提交回复
热议问题