C++: Using Wide Char Array Strings

Using Wide Char Array Strings

In this C++ video tutorial, we demonstrate how to use Unicode-based wide-character array strings to perform common string operations, such as input, get length, copy, concatenation, tokenize, and compare. These operations perform almost exactly as they do for ASCII-based char array strings. As such, we cover the material quickly.

Basically, all of the functions for working with wide-character array strings are in the library <cwchar>. This file is roughly the equivalent of <cstring> for wide char array strings. However, <cwchar> also contains additional functions for converting wide strings to double and integer values, similar to those in <cstdlib> for char array strings. Some extra functions for determining the type of a wide char are in the library <cwctype>.

#include <iostream>
#include <cwchar>

int main() {
    using namespace std;

    wchar_t waPi[] = L"3.14159";

    // Call the conversion function wide char string
    // to double with NULL as the end ptr
    double dPi = wcstod(waPi, NULL);
    cout << "Converted value = " << dPi << endl;

    return 0;
}

The code above gives a quick demonstration of how to convert a wide char string to a double value using the function wcstod() in the <cwchar> library. This function is similar to the function atof() in <cstdlib> for char array strings.

The program below demonstrates how to sort an array of wide char array strings by using the comparison function wcscmp() from <cwchar> and the quick sort function qsort() from <cstdlib>. In this program, we have a list of four novels by the Russian-born American novelist Ayn Rand. To sort the list with qsort(), we need a comparison function with the correct signature. So, we have wrapped the function wcscmp() inside of our comparison function Compare(), which takes void pointer arguments.

#include <iostream>
#include <cwchar>
#define NNovelCount 4

int Compare(const void* cvpWStr1, const void* cvpWStr2) {
    return wcscmp((const wchar_t*)cvpWStr1, (const wchar_t*)cvpWStr2);
}

int main() {
    using namespace std;

    // An array novels by Ayn Rand
    wchar_t waaNovels[NNovelCount][20] = {
        L"We the Living",
        L"Atlas Shrugged",
        L"The Fountainhead",
        L"Anthem"
    };

    // Sort the array using qsort()
    qsort(waaNovels, NNovelCount, 20*sizeof(wchar_t), Compare);
    for (int i = 0; i < NNovelCount; ++i) {
        wcout << waaNovels[i] << endl;
    }

    return 0;
}

Here, we see the sorted list outputted from our program. One subtle point about the code: we use 20 times the size of wchar_t as the size of our entries. This works, but it is much more efficient to make an array of pointers to wide char arrays strings and sort that instead. We have chosen to give the code above instead because it is simpler and more understandable to implement.

Before the qsort() function is called, we have an unsorted list of Ayn Rand's novels, which looks like this:

  1. We the Living
  2. Atlas Shrugged
  3. The Fountainhead
  4. Anthem

After qsort() is called, we get the output of the sorted list of Ayn Rand's novels, which looks like this:

  1. Anthem
  2. Atlas Shrugged
  3. The Fountainhead
  4. We the Living