cplusplus.com
C++ : Reference : Miscellaneous : locale : collate : compare
 
cplusplus.com
Information
Documentation
Reference
Articles
Forum
Reference
C Library
IOstream Library
Strings library
STL Containers
STL Algorithms
Miscellaneous
Miscellaneous
complex
exception
functional
iterator
limits
locale
memory
new
numeric
stdexcept
typeinfo
utility
valarray
locale
has_facet
isalnum
isalpha
iscntrl
isdigit
isgraph
islower
isprint
ispunct
isspace
isupper
isxdigit
locale
tolower
toupper
use_facet
standard facets:
codecvt
codecvt_base
codecvt_byname
collate
collate_byname
ctype
ctype_base
ctype_byname
messages
messages_base
messages_byname
moneypunct
moneypunct_byname
money_base
money_get
money_put
numpunct
numpunct_byname
num_get
num_put
time_base
time_get
time_get_byname
time_put
time_put_byname
collate
collate::collate
public member functions:
collate::compare
collate::hash
collate::transform
public member types:
collate::char_type
collate::string_type
protected members:
collate::do_compare
collate::do_hash
collate::do_transform
collate::~collate


collate::compare

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

Compare character sequences

Compares the character sequence in the range [low1,high1) to the one in [low2,high2), returning 1 if the whole first sequence is considered greater than the second one, or -1 if it is considered less. Otherwise, if neither is considered greater o less than the other, they are considered equal and a value of 0 is returned.

Multi-byte characters are compared appropriately.

For the standard specialization of char and wchar_t the function returns the same as lexicographical_compare, which performs a character code (ASCII) comparison between the individual characters of both sequences, returning an alphabetical ordering for alphabetical strings.

During its operation, the version of this function in the generic template simply calls the virtual protected member do_compare, which is the member function in charge of performing the actions described above.

Parameters

low1, high1
Pointer to the beginning and ending characters of the first sequence. The range used is [low1,high1), which contains all the characters between low1 and high1, including the character pointed by low1 but not the character pointed by high1.
charT is the template parameter (i.e., the facet's character type).
low2, high2
Pointer to the beginning and ending characters of the second sequence. The range used is [low2,high2).


Return value

1 if the first string is greater than the second.
-1 if the first string is less than the second.
0 otherwise (i.e., they are considered equal).

Example

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
// collate::compare example
#include <iostream>
#include <locale>
using namespace std;

int main ()
{
  char first[]="STRAWBERRY";  // c-string
  string second="BLUEBERRY";  // string

  locale loc;                 // get "C" locale

  // get collate facet:
  const collate<char>& coll = use_facet<collate<char> >(loc);

  int result = coll.compare ( first, first+10,
                              second.data(), second.data()+second.length() );
  cout << first;
  switch (result) {
    case 1:
      cout << " is greater than "; break;
    case 0:
      cout << " is equal to "; break;
    case -1:
      cout << " is less than "; break;
  }
  cout << second << endl;
  return 0;
} 


Output:

STRAWBERRY is greater than BLUEBERRY

See also