How can I use std::maps with user-defined types as key?

前端 未结 7 1765
灰色年华
灰色年华 2020-11-22 04:30

I\'m wondering why I can\'t use STL maps with user-defined classes. When I compile the code below, I get the following cryptic error message. What does it mean? Also, why is

7条回答
  •  一整个雨季
    2020-11-22 05:04

    By default std::map (and std::set) use operator< to determine sorting. Therefore, you need to define operator< on your class.

    Two objects are deemed equivalent if !(a < b) && !(b < a).

    If, for some reason, you'd like to use a different comparator, the third template argument of the map can be changed, to std::greater, for example.

提交回复
热议问题