Udostępnij za pośrednictwem


ctype::narrow

Converts characters of type CharType used by a locale to the corresponding characters of type char in the native character set.

This method is potentially unsafe, as it relies on the caller to check that the passed values are correct. Consider using ctype::_Narrow_s instead.

char narrow(
    CharType ch, 
    char default = '\0'
) const;
const CharType* narrow(
    const CharType* first, 
    const CharType* last,
    char default, 
    char* dest
) const;

Parameters

  • ch
    The character of type Chartype used by the locale to be converted.

  • default
    The default value to be assigned by the member function to characters of type CharType that do not have counterpart characters of type char.

  • first
    A pointer to the first character in the range of characters to be converted.

  • last
    A pointer to the character immediately following the last character in the range of characters to be converted.

  • dest
    A const pointer to the first character of type char in the destination range that stores the converted range of characters.

Return Value

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.

Remarks

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 ( widenc ), 0 ) == c.

Note

Any specialization of ctype that overrides this method must also override the ctype::_Narrow_s method.

Example

// 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];
   // Note:: ctype::narrow is potentially unsafe, consider
   // using ctype::_Narrow_s instead.
   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

Requirements

Header: <locale>

Namespace: std

See Also

Reference

ctype Class

Other Resources

ctype Members

Change History

Date

History

Reason

March 2009

Corrected.

Customer feedback.