The previous chapters explained how you can use locales and the standard facet classes, and how you can build new facet classes. This chapter introduces you to the technique of building your own facet class and using it in conjunction with the input/output streams of the C++ Standard Library, the iostreams. This material is rather advanced, and requires some knowledge of standard iostreams.
In the following sections, we will work through a complete example on formatting telephone numbers. Formatting telephone numbers involves local conventions that vary from culture to culture. For example, the same US phone number can have all of the formats listed below:
754-3010 | Local |
(541) 754-3010 | Domestic |
+1-541-754-3010 | International |
1-541-754-3010 | Dialed in the US |
001-541-754-3010 | Dialed from Germany |
191 541 754 3010 | Dialed from France |
Now consider a German phone number. Although a German phone number consists of an area code and an extension like a US number, the format is different. Here is the same German phone number in a variety of formats:
636-48018 | Local |
(089) / 636-48018 | Domestic |
+49-89-636-48018 | International |
19-49-89-636-48018 | Dialed from France |
Note the difference in formatting domestic numbers. In the US, the convention is 1 (area code) extension, while in Germany it is (0 area code)/extension.