#include <iostream>
#include <list>
#include <functional>
#include <algorithm>
using namespace std;
// A simple function object.
class reciprocal: unary_function<double, double> {
public:
result_type operator()(argument_type i) {
return (result_type) 1.0/i; // return reciprocal
}
};
int main()
{
list<double> listObject;
int i;
for(i = 1; i <10; i++)
listObject.push_back((double)i);
cout << "Original contents of listObject:\n";
list<double>::iterator p = listObject.begin();
while(p != listObject.end()) {
cout << *p << " ";
p++;
}
cout << endl;
// use reciprocal function object
// call function object
p = transform(listObject.begin(), listObject.end(), listObject.begin(), reciprocal());
cout << "Transformed contents of listObject:\n";
p = listObject.begin();
while(p != listObject.end()) {
cout << *p << " ";
p++;
}
return 0;
}
|