Collation Sample

Description

Collation refers to numerical orderings of characters, and collating sequences are used to sort strings of data. The C++ Standard Library contains several mechanisms that support collation in international contexts.

One of the simplest uses of collation is applied with std::sort():

	sort(list, list + 4);  		// uses default locale

	locale loc = locale("fr_ca");
	sort(list, list + 4, loc);	// uses specified locale
That is, the collating rules of the given locale are used to actually order the strings during the sort.

Another technique involves use of the collate::compare() function, a lower-level mechanism for comparing sequences of characters.

Finally, collate::transform() is used to convert a character sequence into a string, such that comparing the strings will yield the same result as collate::compare() applied to the underlying character sequences used as input to collate::transform(). The idea with transform() is that for some character sets, doing lexicographical comparisons may be expensive, due to a need to transform individual characters. For example, a specific character may be ignored in comparisons, or ranges of characters may not be contiguous for comparison purposes.

Concept

The sample sorts a list of strings according to a specified locale, then compares two character sequences according to the collating sequence for the given locale. Finally, a character sequence is converted to a string, and the string is looked up in a list of strings that have been converted using collate::transform().

Supported
Supported
Supported