|
发表于 2003-7-27 10:56:49
|
显示全部楼层
例4(类似例2):
// Define a class
#include <string>
#include <iostream>
#include <algorithm>
#include <cctype>
#include <clocale>
using namespace std;
class my_tolower
{
public:
char operator()(char c) const
{
return tolower(static_cast<const char>(c));
}
};
class my_toupper
{
public:
char operator()(char c) const
{
return toupper(static_cast<const char>(c));
}
};
int main()
{
// input a string
cout << "lease input a string: ";
string s;
cin >> s;
cout << "original: " << s << endl;
setlocale(LC_ALL, "");
// lowercase all characters
transform (s.begin(), s.end(), // source
s.begin(), // destination
my_tolower()); // operation
cout << "lowered: " << s << endl;
// uppercase all characters
transform (s.begin(), s.end(), // source
s.begin(), // destination
my_toupper()); // operation
cout << "uppered: " << s << endl;
} |
|