Using the less than comparison operator for strings

后端 未结 2 1653
执笔经年
执笔经年 2020-11-30 11:18

I\'m following a tutorial for C++ and looking at strings and overloading with operators such as +=, ==, != etc. Currently I have a sim

相关标签:
2条回答
  • 2020-11-30 11:39

    The comparison operators implement lexicographic ordering of strings.

    -= and *= are not defined for strings.

    0 讨论(0)
  • 2020-11-30 11:44

    The less-than operator on strings does a lexicographical comparison on the strings. This compares strings in the same way that they would be listed in dictionary order, generalized to work for strings with non-letter characters.

    For example:

    "a" < "b"
    "a" < "ab"
    "A" < "a"             (Since A has ASCII value 65; a has a higher ASCII value)
    "cat" < "caterpillar"
    

    For more information, look at the std::lexicographical_compare algorithm, which the less-than operator usually invokes.

    As for -= and *=, neither of these operators are defined on strings. The only "arithmetic" operators defined are + and +=, which perform string concatenation.

    Hope this helps!

    0 讨论(0)
提交回复
热议问题