Create a reciprocal function object with transform() and customized function
#include <iostream>
#include <list>
#include <functional>
#include <algorithm>
using namespace std;
class reciprocal: unary_function<double, double> {
public:
result_type operator()(argument_type i)
{
return (result_type) 1.0/i;
}
};
int main()
{
list<double> l;
int i;
for(i=1; i<10; i++) l.push_back((double)i);
list<double>::iterator p = l.begin();
p = transform(l.begin(), l.end(),l.begin(),reciprocal());
p = l.begin();
while(p != l.end()) {
cout << *p << " ";
p++;
}
return 0;
}
Related examples in the same category