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



In article <gp3543tv3hm1a44ihbh67e6lbfvtjo4321@xxxxxxx>, dsh@xxxxxxxx
says...

[ ... ]

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.

This is one place that C++ does quite a bit better job than C. In C you
have only a single, global locale that applies to everything at any
given time.

C++ has a global locale (inherited from C) that applies to things like
the C string functions, but it also allows you to associate locale
objects with individual items so you can (usually) avoid the scenario
you describe above, having to constantly change the global locale. For
example, each iostream has an associated locale.

Unfortunately, in the case of strings, the locale-like information for
the string is an implicit property of the char_traits class, so it's not
entirely straightforward to do a string comparison using the ordering
from a specific locale. Worse, the char_traits for a string is a
template parameter, so two strings that use different char_traits are
completely separate types and you can't compare one to another, assign
one to another, etc. That's not always a major problem, but sometimes it
becomes a serious pain.

Finally, the simple fact is that most documentation on this part of the
C++ standard library just plain sucks. Josuttis covers it pretty well,
but I haven't seen much else that could even claim mediocre coverage.
This is also an area that's decidedly different from the STL-inspired
parts of the library -- this is a much more object-oriented setup, where
doing almost anything typically involves deriving a new class from one
of the base classes in the library. In quite a few cases, the overhead
can seem a bit ridiculous.

--
Later,
Jerry.

The universe is a figment of its own imagination.
.



Relevant Pages

  • RfD: Internationalisation
    ... 2007-06-26 Updated rationale section, LOCALE@, and minor wordsmithing ... text files that can be edited and converted to another language ... in a similar way to the ANS word C", but returns a string identifier ... We use the word locale to mean the mixture of country, language, ...
    (comp.lang.forth)
  • Re: datediff() and isdate() not working with hungarian date format (VB6)
    ... When I set in the control panel Hungry and then change the short date format ... Then Datediffand and isdate() do not work with strings. ... Dim t1 As String ... Most of the VB run-time functions are aware of your locale. ...
    (microsoft.public.vb.bugs)
  • Re: datediff() and isdate() not working with hungarian date format (VB6)
    ... set as date format on theirs computers. ... Then Datediffand and isdate() do not work with strings. ... Dim t1 As String ... Most of the VB run-time functions are aware of your locale. ...
    (microsoft.public.vb.bugs)
  • Re: datediff() and isdate() not working with hungarian date format (VB6)
    ... Then Datediffand and isdate() do not work with strings. ... Dim t1 As String ... Most of the VB run-time functions are aware of your locale. ...
    (microsoft.public.vb.bugs)
  • Re: Writing Japanese or Chinese strings in a text file
    ... what locale are you running in? ... So they are right in the excel file. ... > original characters). ... web page (exactly I put with VB the string from a textarea in a chinese ...
    (microsoft.public.vb.general.discussion)

Loading