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