//: C04:Locale.cpp
//{-g++}
//{-bor}
//{-edg}
// Illustrates effects of locales
#include
#include
using namespace std;
int main() {
locale def;
cout << def.name() << endl;
locale current = cout.getloc();
cout << current.name() << endl;
float val = 1234.56;
cout << val << endl;
// Change to French/France
cout.imbue(locale("french"));
current = cout.getloc();
cout << current.name() << endl;
cout << val << endl;
cout << "Enter the literal 7890,12: ";
cin.imbue(cout.getloc());
cin >> val;
cout << val << endl;
cout.imbue(def);
cout << val << endl;
} ///:~
Here’s the output:
C
C
1234.56
French_France.1252
1234,56
Enter the literal 7890,12: 7890,12
7890,12
7890.12
The default locale is the "C" locale, which is what C and C++ programmers have been used to all these years (basically, English language and American culture). All streams are initially "imbued" with the "C" locale. The imbue( )member function changes the locale that a stream uses. Notice that the full ISO name for the "French" locale is displayed (that is, French used in France vs. French used in another country). This example shows that this locale uses a comma for a radix point in numeric display. We have to change cinto the same locale if we want to do input according to the rules of this locale.
Each locale category is divided into number of facets , which are classes encapsulating the functionality that pertains to that category. For example, the timecategory has the facets time_putand time_get, which contain functions for doing time and date inputand outputrespectively. The monetarycategory has facets money_get, money_put, and moneypunct. (The latter facet determines the currency symbol.) The following program illustrates the moneypunctfacet. (The timefacet requires a sophisticated use of iterators which is beyond the scope of this chapter.)
//: C04:Facets.cpp
//{-bor}
//{-g++}
#include
#include
#include
using namespace std;
int main() {
// Change to French/France
locale loc("french");
cout.imbue(loc);
string currency =
use_facet >(loc).curr_symbol();
char point =
use_facet >(loc).decimal_point();
cout << "I made " << currency << 12.34 << " today!"
<< endl;
} ///:~
The output shows the French currency symbol and decimal separator:
I made Ç12,34 today!
You can also define your own facets to construct customized locales. [47] See the Langer & Kreft book mentioned earlier for more detailed information.
Be aware that the overhead for locales is considerable. In fact, some library vendors provide different "flavors" of the standard C++ library to accommodate environments that have limited space. [48] See, for example, Dinkumware’s Abridged library at www.dinkumware.com. This library omits locale support. and exception support is optional.
This chapter has given you a fairly thorough introduction to the iostream class library. In all likelihood, it is all you need to create programs using iostreams. However, be aware that some additional features in iostreams are not used often, but you can discover them by looking at the iostream header files and by reading your compiler’s documentation on iostreams or the references mentioned in this chapter and in the book’s preface .
1. Open a file by creating an ifstreamobject. Make an ostringstreamobject and read the entire contents into the ostringstreamusing the rdbuf( )member function. Extract a stringcopy of the underlying buffer and capitalize every character in the file using the Standard C toupper( )macro defined in . Write the result out to a new file.</#>
24. Create a program that opens a file (the first argument on the command line) and searches it for any one of a set of words (the remaining arguments on the command line). Read the input a line at a time, and write out the lines (with line numbers) that match to the new file.
25. Write a program that adds a copyright notice to the beginning of all source-code files indicated by the program’s command-line arguments.
26. Use your favorite text-searching program ( grep, for example) to output the names (only) of all the files that contain a particular pattern. Redirect the output into a file. Write a program that uses the contents of that file to generate a batch file that invokes your editor on each of the files found by the search program.
27. We know that setw( )allows for a minimum of characters read in, but what if you wanted to read a maximum? Write an effector that allows the user to specify a maximum number of characters to extract. Have your effector also work for output, in such a way that output fields are truncated, if necessary, to stay within width limits.
28. Demonstrate to yourself that if the fail or bad bit is set, and you subsequently turn on stream exceptions, that the stream will immediately throw an exception.
29. String streams accommodate easy conversions, but they come with a price. Write a program that races atoi( )against the stringstreamconversion system to see the effect of the overhead involved with stringstream.
30. Make a Personstruct with fields such as name, age, address, etc. Make the string fields fixed-size arrays. The social security number will be the key for each record. Implement the following Databaseclass.
class DataBase {
public:
// Find where a record is on disk
size_t query(size_t ssn);
// Return the person at rn (record number)
Person retrieve(size_t rn);
// Record a record on disk
void add(const Person& p);
};
Write some Personrecords to disk (do not keep them all in memory). When the user requests a record, read it off the disk and return it. The I/O operations in the DataBaseclass use read( )and write( )to process all Personrecords.
31. Write an operator 47", " ");
replace_if(v.begin(), v.end(),
bind2nd(greater_equal(), 7), -1);
print(v.begin(), v.end(), "replace_if >= 7 -> -1", " ");
} ///:~
The example begins with two predicates: PlusOne,which is a binary predicate that returns trueif the second argument is equivalent to one plus the first argument; and MulMoreThan,which returns trueif the first argument times the second argument is greater than a value stored in the object. These binary predicates are used as tests in the example.
In main( ), an array ais created and fed to the constructor for vector v. This vector is used as the target for the search and replace activities, and you’ll note that there are duplicate elements—these are discovered by some of the search/replace routines .
The first test demonstrates find( ), discovering the value 4 in v. The return value is the iterator pointing to the first instance of 4, or the end of the input range ( v.end( )) if the search value is not found .
Читать дальше