ctype::narrow
Converts characters of type CharType used by a locale to the corresponding characters of type char in the native character set.
char narrow(
CharType ch,
char default = '\0'
) const;
const CharType* narrow(
const CharType* first,
const CharType* last,
char default,
char* dest
) const;
The first member function returns the native character of type char that corresponds to the parameter character of type CharType default if not counterpart is defined.
The second member function returns a pointer to the destination range of native characters converted from characters of type CharType.
The first member function returns do_narrow(ch, default). The second member function returns do_narrow (first, last, default, dest). Only the basic source characters are guaranteed to have a unique inverse image CharType under narrow. For these basic source characters, the following invariant holds: narrow ( widen ( c ), 0 ) == c.
// ctype_narrow.cpp
// compile with: /EHsc /W3
#include <locale>
#include <iostream>
using namespace std;
int main( )
{
locale loc1 ( "english" );
wchar_t *str1 = L"\x0392fhello everyone";
char str2 [16];
bool result1 = (use_facet<ctype<wchar_t> > ( loc1 ).narrow
( str1, str1 + wcslen(str1), 'X', &str2[0] ) != 0); // C4996
str2[wcslen(str1)] = '\0';
wcout << str1 << endl;
cout << &str2[0] << endl;
}
Xhello everyone