C++ cstring strxfrm() Function
Example
Transform two C-style strings, and compare the results with strcmp():
char str1[] = "Apple";
char str2[] = "Banana";
char key1[50], key2[50];
strxfrm(key1, str1, 50);
strxfrm(key2, str2, 50);
cout << (strcmp(key1, key2) < 0 ? "Apple comes first" : "Banana comes first");
Definition and Usage
The strxfrm() function transforms (changes) a C-style string with the sorting rules of the current locale (the language and country settings of the program), and copies the result into a char array.
If you compare two transformed strings with strcmp(), you get the same result as when you compare the original strings with strcoll(). This is faster when you compare the same strings many times, for example when you sort them.
The strxfrm function is defined in the <cstring> header file.
Note: The transformed string is not meant to be printed. What it looks like depends on the system, so only use it to compare strings.
Syntax
strxfrm(char* destination, const char* source, size_t maxLength);
The size_t data type is a non-negative integer.
Parameter Values
| Parameter | Description |
|---|---|
| destination | Required. The char array to write the transformed string to. |
| source | Required. The string to transform. |
| maxLength | Required. The maximum number of characters to write to destination, including the null character (\0). |
Technical Details
| Returns: | The length of the transformed string (without the null character), as a size_t. If this number is equal to or larger than maxLength, the array was too small, and you cannot use its content. |
|---|