Reference
C Library
IOstream Library
Strings library
STL Containers
STL Algorithms
Miscellaneous
Miscellaneous
complex
exception
functional
iterator
limits
locale
memory
new
numeric
stdexcept
typeinfo
utility
valarray
locale
has_facet
isalnum
isalpha
iscntrl
isdigit
isgraph
islower
isprint
ispunct
isspace
isupper
isxdigit
locale
tolower
toupper
use_facet
standard facets:
codecvt
codecvt_base
codecvt_byname
collate
collate_byname
ctype
ctype_base
ctype_byname
messages
messages_base
messages_byname
moneypunct
moneypunct_byname
money_base
money_get
money_put
numpunct
numpunct_byname
num_get
num_put
time_base
time_get
time_get_byname
time_put
time_put_byname
ctype
ctype::ctype
public member functions:
ctype::is
ctype::narrow
ctype::scan_is
ctype::scan_not
ctype::tolower
ctype::toupper
ctype::widen
public member types:
ctype::char_type
ctype::mask
protected members:
ctype::do_is
ctype::do_narrow
ctype::do_scan_is
ctype::do_scan_not
ctype::do_tolower
ctype::do_toupper
ctype::do_widen
ctype::~ctype


ctype::narrow

public member function
        char narrow (charT c, char dfault) const;
const charT* narrow (const charT* low, const charT* high, char dfault, char* to) const;

Narrow character(s)

The first version returns the transformation of c (generally a wide character value) to its equivalent of type char, if such an equivalent exists, or dfault otherwise.

The second version fills the range starting at to with the char transformations of the characters in the range [low,high).

The transformation applied is the simplest reasonable transformation from the facet character type (charT, its template parameter) to the fundamental narrow character type char.

The transformation preserves the character categories (as specified by ctype::mask), whenever the character is not dfault.

During its operation, this function simply calls the virtual protected member ctype::do_narrow, which is the member function in charge of performing the actions described above.

Parameters

c
Character to transform.
charT is the template parameter of ctype (i.e., the facet's character type).
low, high
Pointer to the initial and final characters of the sequence. The range used is [low,high), which contains all the characters between low and high, including the character pointed by low but not the character pointed by high.
charT is the template parameter of ctype (i.e., the facet's character type).
dfault
Default char value to be used for any character that cannot be mapped to a char equivalent from a charT value.
to
Pointer to a range of elements of type char able to store at least as many elements as the range between low and high.
Notice that the type of this parameter is a pointer to an array of elements of type char, independently of the character type the facet uses.

Return value

The first version returns the transformation of c.

The second version returns high.

charT is the template parameter of ctype (i.e., the facet's character type).

Example

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
// ctype::narrow example
#include <iostream>
#include <locale>
#include <string>
using namespace std;
int main ()
{
  locale loc;
  wstring yourname;
  cout << "Please enter your name: ";
  getline (wcin,yourname);
  int length = yourname.length();
  cout << "The first (narrow) character in your name is: ";
  char c = use_facet< ctype<wchar_t> >(loc).narrow ( yourname[0], '?' );
  cout << c << endl;
  cout << "The narrow transformation of your name is: ";
  char * pc = new char [length+1];
  use_facet< ctype<wchar_t> >(loc).narrow ( yourname.c_str(), yourname.c_str()+length+1, '?', pc);
  cout << pc << endl;
  return 0;
}


Output:

Please enter your name: Juan SouliƩ
The first (narrow) character in your name is: J
The narrow transformation of your name is: Juan SouliƩ

See also