|
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
|