Click on the banner to return to the class reference home page.

collate, collate_byname


collate_bynamecollatelocale::facet

Summary

String collation, comparison, and hashing facet.

Data Type and Member Function Indexes
(exclusive of constructors and destructors)

Synopsis

#include <locale>
template <class charT> class collate;
template <class charT> class collate_byname;

Description

The collate and collate_byname facets provides string collation, comparison, and hashing facilities. collate provides these facilities for the "C" locale, while collate_byname provides the same thing for named locales.

Interface

template <class 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*, const charT*,
              const charT*, const charT*) const;
  string_type transform(const charT*, const charT*) const;
  long hash(const charT*, const charT*) const;
  static locale::id id;
protected:
  ~collate();  // virtual
  virtual int do_compare(const charT*, const charT*,
                         const charT*, const charT*) const;
  virtual string_type do_transform(const charT*, const charT*) const;
  virtual long do_hash (const charT*, const charT*) const;
};

template <class charT>
class collate_byname : public collate<charT> {
public:
  explicit collate_byname(const char*, size_t = 0);
protected:
  ~collate_byname();  // virtual
  virtual int do_compare(const charT*, const charT*,
                         const charT*, const charT*) const;
  virtual string_type do_transform(const charT*, const charT*) const;
  virtual long do_hash(const charT*, const charT*) const;
};

Types

char_type
string_type

Constructors and Destructors

explicit collate(size_t refs = 0)
explicit collate_byname(const char* name, size_t refs = 0);
~collate();  // virtual and protected
~collate_byname();  // virtual and protected

Facet ID

static locale::id id;

Public Member Functions

The public members of the collate facet provide an interface to protected members. Each public member xxx has a corresponding virtual protected member do_xxx. All work is delegated to these protected members. For instance, the long version of the public grouping function simply calls its protected cousin do_grouping.

int 
compare(const charT* low1, const charT* high1,
        const charT* low2, const charT* high2) const;

long 
hash(const charT* low, const charT* high) const;

string_type 
transform(const charT* low, const charT* high) const;

Protected Member Functions

virtual int    
do_compare(const charT* low1, const charT* high1,
           const charT* low2, const charT* high2) const;
virtual long   
do_hash( const charT* low, const charT* high)
virtual string_type 
do_transform(const charT* low, const charT* high) const;

Example

//
// collate.cpp
//
#include <iostream>

int main ()
{
  using namespace std;
  locale loc;
  string s1("blue");
  string s2("blues");
  // Get a reference to the collate<char> facet
  const collate<char>& co =
#ifndef _RWSTD_NO_TEMPLATE_ON_RETURN_TYPE
      use_facet<collate<char> >(loc);
#else
      use_facet(loc,(collate<char>*)0);
#endif
  // Compare two strings
  cout << co.compare(s1.begin(),s1.end(),
                     s2.begin(),s2.end()-1) << endl;
  cout << co.compare(s1.begin(),s1.end(),
                     s2.begin(),s2.end()) << endl;
  // Retrieve hash values for two strings
  cout << co.hash(s1.begin(),s1.end()) << endl;
  cout << co.hash(s2.begin(),s2.end()) << endl;
  return 0;
}

See Also

locale, facets, ctype


©Copyright 1996, Rogue Wave Software, Inc.