Create a reciprocal function object with transform() and customized function : count « STL Algorithms Non modifying sequence operations « C++






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

1.count true value in a vector
2.Use count on string
3.Use count function and print elements with value 4
4.Use count function to count elements with even value
5.Use count function to count elements that are greater than value 4
6.Use count() with vector of boolean value