Why is inequality tested as (!(a==b)) in a lot of C++ standard library code?

前端 未结 6 1799
忘了有多久
忘了有多久 2021-02-01 11:24

This is the code from the C++ standard library remove code. Why is inequality tested as if (!(*first == val)) instead of if (*first != val)

6条回答
  •  有刺的猬
    2021-02-01 12:14

    Most functions in STL work only with operator< or operator==. This requires the user only to implement these two operators (or sometimes at least one of them). For example std::set uses operator< (more precisely std::less which invokes operator< by default) and not operator> to manage ordering. The remove template in your example is a similar case - it uses only operator== and not operator!= so the operator!= doesn't need to be defined.

提交回复
热议问题