std::map Requirements for Keys (Design Decision)

后端 未结 5 1200
天涯浪人
天涯浪人 2021-02-13 18:24

When I make a std::map, what C++ expects from me is that my_data_type has its own operator<.

5条回答
  •  挽巷
    挽巷 (楼主)
    2021-02-13 19:07

    Your assumptions aren't correct. Here's what's really happening:

    std::map is a class template which takes four template parameters: key type K, mapped type T, comparator Comp and allocator Alloc (the names are immaterial, of course, and only local to this answer). What matters for this discussion is that an object Comp comp; can be called with two key refrences, comp(k1, k2), where k1 and k2 are K const &, and the result is a boolean which imlpements a strict weak ordering.

    If you do not specify the third argument, then Comp is the default type std::less, and this (stateless) class imlpements the binary operation as k1 < k2. It does not matter whether this <-operator is a member of K, or a free function, or a template, or whatever.

    And that wraps up the story, too. The comparator type is the only datum required to implement an ordered map. Equality is defined as !comp(a, b) && !comp(b,a), and the map only stores one unique key according to this definition of equality.

    There is no reason to make additional requirements on the key type, and also there is no logical reason that a user-defined operator== and operator< should at all be compatible. They could both exist, independently, and serve entirely different and unrelated purpose.

    A good library imposes the minimal necessary requirements and offers the greatest possible amount of flexibility, and this is precisely what std::map does.

提交回复
热议问题