Re: sorting std::vector<string> ignoring case
- From: Jerry Coffin <jcoffin@xxxxxxxxx>
- Date: Thu, 10 May 2007 09:05:32 -0600
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.
.
- Follow-Ups:
- Re: sorting std::vector<string> ignoring case
- From: Doug Harrison [MVP]
- Re: sorting std::vector<string> ignoring case
- References:
- sorting std::vector<string> ignoring case
- From: Ed
- Re: sorting std::vector<string> ignoring case
- From: Darko Miletic
- Re: sorting std::vector<string> ignoring case
- From: Cholo Lennon
- Re: sorting std::vector<string> ignoring case
- From: Doug Harrison [MVP]
- Re: sorting std::vector<string> ignoring case
- From: Cholo Lennon
- Re: sorting std::vector<string> ignoring case
- From: Doug Harrison [MVP]
- sorting std::vector<string> ignoring case
- Prev by Date: Re: sorting std::vector<string> ignoring case
- Next by Date: Re: sorting std::vector<string> ignoring case
- Previous by thread: Re: sorting std::vector<string> ignoring case
- Next by thread: Re: sorting std::vector<string> ignoring case
- Index(es):
Relevant Pages
|
Loading