public member function
<locale>
std::collate::compareint compare (const char_type* low1, const char_type* high1 const char_type* low2, const char_type* high2) const;
Compare character sequences
Compares the character sequence in the range [low1,high1) to the one in [low2,high2), returning1
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.
Internally, this function simply calls the virtual protected member do_compare, which by default performs a simple lexicographical comparison for the standard specializations (comparing the char
or wchar_t
values directly).
A lexicographical comparison is the kind of comparison generally used to sort words alphabetically in dictionaries; It involves comparing sequentially the elements that have the same position in both ranges against each other until one element is not equivalent to the other. The result of comparing these first non-matching elements is the result of the lexicographical comparison.
[low1,high1)
, which contains all the characters between low1 and high1, including the character pointed by low1 but not the character pointed by high.
[low2,high2)
.
-1
The first sequence is less than the second (i.e., the first goes before the second in alphabetical order). 0
Both sequences are considered equal. 1
The first sequence is greater than the second (i.e., the first goes after the second in alphabetical order).
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> // std::cout
#include <string> // std::string
#include <locale> // std::locale, std::collate, std::use_facet
int main ()
{
char first[]="STRAWBERRY"; // c-string
std::string second="BLUEBERRY"; // standard string
std::locale loc; // get "C" locale
// get collate facet:
const std::collate<char>& coll = std::use_facet<std::collate<char> >(loc);
int result = coll.compare (first, first+10,
second.data(), second.data()+second.length());
std::cout << first;
switch (result) {
case 1:
std::cout << " is greater than "; break;
case 0:
std::cout << " is equal to "; break;
case -1:
std::cout << " is less than "; break;
}
std::cout << second << '\n';
return 0;
}
STRAWBERRY is greater than BLUEBERRY
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4