shablool wrote:
Unfortunately, std::string lacks case insensitive compare.
Case-insensitive operations are locale dependant, and therefore require
a more complex functionality then the one described above.See:
Actually it's the char_traits that performs the comparison, not the
std::basic_string class.
You can use a specific char_traits in your strings to do such
comparisons.
transforming the whole string looks wrong algorithmically, as it means
you have "worst case scenario" on every string, i.e. the slowest result
of the comparison is either when the strings match or they differ on
the last character, in which case you need N comparisons, but a lot of
the time the comparison will fail on the first character. By
transforming the entire string to lowercase (or uppercase) your
algorithm will be O(N) every time (well you will be performing an
action on every character).
It would be therefore better to provide a single-character comparison
and use that.
I would like to see a case-insensitive comparison function in <locale>
which is where I think it belongs. There isn't one, although you can
write one based on the toupper that lives in locale. You can do it
based on single-character comparison (call the locale toupper or
tolower on both and compare them) and then you can do another one for a
sequence (std::compare with your predicate).
Difficult to know what header it should be in - it is both an algorithm
and locale based. You wouldn't want to put it in <algortihm> because it
has then has a dependency on <locale>. You could do it the other way if
you rewrite the compare and not use std::compare. The other alternative
would be to give it its own header so you only include what you use.