Generic For Each
template <class I, class F>
F g_for_each (I beg, I end, F f)
{
for ( ; beg!= end; ++beg)
f(*beg);
return f;
}
|
class SmartMakeUpperCase
{
public:
int count;
SmartMakeUpperCase () : count(0) {}
void operator () (char& x)
{
char y = x;
x = toupper(x);
if (y != x) ++count;
}
};
|
// usage:
SmartMakeUpperCase smuc0, smuc1;
smuc1 = g_for_each (L.Begin(), L.End(), smuc0); // counts the conversions
cout << smuc1.count - smuc0.count; // number of letters in L converted to upper case
|