Re: sorting std::vector<string> ignoring case



On Wed, 9 May 2007 00:18:22 -0300, "Cholo Lennon" <chololennon@xxxxxxxxxxx>
wrote:

I'm sorry for my wrong expression, with 'portable' I wanted to say ANSI compatible. Also I didn't say that the original names has
been removed. They exist yet. Still is valid to use them in VC8 but, like you appointed, they are deprecated.

This is extremely nitpicking, but I believe stricmp and _stricmp are
equally "ANSI compatible", and I gave the reason in the "P.S." part of my
last message. I think the important point is to realize that while declared
in <string.h>, neither are Standard C functions.

FWIW, I mentioned using the Windows function CompareString in my other
message largely because string comparison seems to get more and more
intricate every day, and Windows goes quite a bit beyond C locales, which
influence the behavior of functions like stricmp. For example, I used to
think that doing a setlocale(LC_ALL, "") at program startup was a necessary
first step without any ramifications, but then I looked at how to compare
English strings when searching a presorted data table. Naively, I thought
stricmp was good enough, but then I came across a case in which words were
separated by underscores. Not knowing how underscores collate with other
characters under all conditions, I came across CompareString, which says:

http://msdn2.microsoft.com/en-us/library/ms647476.aspx
<q>
When the results of the comparison should be consistent regardless of
locale, for example, when comparing retrieved data against a predefined
list or an internal value, use CompareString or CompareStringEx. Either of
the following calls will match even if mystr is "INLAP", whereas the
locale-sensitive call to lstrcmpi fails if the current locale is
Vietnamese.
</q>

So apparently even pure English doesn't sort the same in a case-insensitive
way across all locales when using functions like lstrcmpi and probably by
extension, stricmp. So to use stricmp in this scenario, I'd have to undo
and reset my C-level locale for every comparison. What a mess. I replaced
it with the "invariant" code suggested by the CompareString documentation.
I'd be interested to hear from anyone who knows how to approach this with
C++ locales.

--
Doug Harrison
Visual C++ MVP
.



Relevant Pages

  • Re: sorting std::vector ignoring case
    ... but I believe stricmp and _stricmp are ... I mentioned using the Windows function CompareString in my other ... locale-sensitive call to lstrcmpi fails if the current locale is ... way across all locales when using functions like lstrcmpi and probably by ...
    (microsoft.public.vc.stl)
  • Re: sorting std::vector ignoring case
    ... So apparently even pure English doesn't sort the same in a case-insensitive ... way across all locales when using functions like lstrcmpi and probably by ... So to use stricmp in this scenario, ... C++ locales. ...
    (microsoft.public.vc.stl)

Loading