Operator < in map template

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Dennis

    #1

    Operator < in map template

    What's the condition for returning true for the less than (<) operator
    between two maps? Is it just based on size? Or does the contents of the
    map matter and if so, how?

    Dennis

  • Thomas Tutone

    #2
    Re: Operator &lt; in map template

    Dennis wrote:
    [color=blue]
    > What's the condition for returning true for the less than (<) operator
    > between two maps? Is it just based on size? Or does the contents of the
    > map matter and if so, how?[/color]

    It's a lexicographical comparison of the maps (just like
    std::lexicograp hical_compare() ). That means an element-by-element
    comparison. According to Josuttis:

    1. When two elements are not equal, the result of their comparison is
    the overal result.
    2. If all elements have been equal so far, the shorter map is less
    than the longer.
    3. If all elements are equal, and the two maps are the same size, then
    the result is false (since the the two maps are actually equal).

    Best regards,

    Tom

    Comment

    • Pete C

      #3
      Re: Operator &lt; in map template

      > It's a lexicographical comparison of the maps (just like[color=blue]
      > std::lexicograp hical_compare() ). That means an element-by-element
      > comparison.[/color]

      Also bear in mind that it's a comparison of map::value_type objects,
      i.e. pair<Key,Value> objects. This means that both the keys and their
      values will affect the comparison, and that operator< must be defined
      for both.

      Comment

      Working...