collate class template — Facet for comparing strings in collation order
template <typename charT> class collate : public locale::facet { public: typedef charT char_type; typedef basic_string<charT> string_type; explicit collate(size_t refs = 0); int compare(const charT* low1, const charT* high1, const charT* low2, const charT* high2) const; string_type transform(const charT* low, const charT* high) const; long hash(const charT* low, const charT* high) const; static locale::id id; protected: virtual ~collate( ); virtual int do_compare(const charT* low1, const charT* high1, const charT* low2, const charT* high2) const; virtual string_type do_transform (const charT* low, const charT* high) const; virtual long do_hash (const charT* low, const charT* high) const; };
The collate
class template
is a facet used to compare strings. In some locales, the collation
order of characters is not the same as the numerical order of their
encodings, and some characters might be logically equivalent even if
they have different encodings.
You can use a locale
object
as a comparator for algorithms that need a comparison function; the
locale's operator( )
function uses the collate
facet to perform the
comparison.
The standard mandates the collate<char>
and collate<wchar_t>
instantiations,
which perform lexicographical (element-wise, numerical) comparison.
See lexicographical_compare
in
<algorithm>
earlier in this
chapter.
As with other facets, the public members call virtual,
protected members with the same name prefaced by do_
. Thus, to use the facet, call the
public functions, such as compare
, which calls do_compare
. The descriptions below are for
the virtual functions because they do the real work. Imagine that
for each virtual function description, there is a corresponding
description for a public, nonvirtual function, such as:
The following are the virtual, protected members of collate
:
virtual
int
do_compare
(const
charT*
low1, const
charT*
high1
, const
charT*
low2,
const
charT*
high2)
const
Compares the character sequences [low1
, high1
) with the character sequence
[low2
, high2
). The return value is one of
the following:
-1
if sequence 1
is less than sequence 2
0
if the
sequences are equal
1
if sequence 1
is greater than sequence 2
virtual
long
do_hash
(const
charT*
low
, const
charT*
high)
const
Returns a hash value for the character sequence
[low
, high
). If do_compare
returns 0
for two character sequences,
do_hash
returns the same
value for the two sequences. The reverse is not necessarily
the case.
virtual
string_type
do_transform
(const
charT*
low, const
charT*
high)
const
Transforms the character sequence [low
, high
) into a string that can be
compared (as a simple lexicographical comparison) with another
transformed string to obtain the same result as calling
do_compare
on the original
character sequences. The do_transform
function is useful if a
program needs to compare the same character sequence many
times.