Is there a convenient way to wrap std::pair as a new type?

后端 未结 8 1176
梦谈多话
梦谈多话 2021-02-02 17:02

Often times I find myself using std::pair to define logical groupings of two related quantities as function arguments/return values. Some examples: row/col, tag/value, etc.

8条回答
  •  刺人心
    刺人心 (楼主)
    2021-02-02 17:31

    You can use some standard utility templates that help define the relation operators.

    #include

    http://www.sgi.com/tech/stl/operators.html

    Requirements on types

    The requirement for operator!= is that x == y is a valid expression
    The requirement for operator> is that y < x is a valid expression
    The requirement for operator<= is that y < x is a valid expression
    The requirement for operator>= is that x < y is a valid expression

    So basically it will automatically generate the other operators give < and == all you have to do is include

提交回复
热议问题