// Example 13-10: Functional to round off a floating point number.
#include <algorithm>
#include <cmath>
#include <functional>
#include <iostream>
#include <iterator>
#include <ostream>
#include <vector>

// Functional for a binary function that rounds off
// a floating point number (of type FltT) to some number
// of decimal places (supplied as an unsigned).
template<typename FltT>
struct roundoff : std::binary_function<FltT,unsigned,FltT> {
  FltT operator()(FltT x, unsigned digits) const {
    FltT y = std::pow(10.0, static_cast<FltT>(digits));
    FltT z = x * y;
    return (z < 0 ? std::ceil(z - 0.5) : std::floor(z + 0.5)) / y;
  }
};

int main()
{
  std::vector<double> seq(3), seq2(3);
  seq[0] = 3.14159;
  seq[1] = 2.71828;
  seq[2] = -3.5;

  // Copy seq to seq2, rounding off to 2 decimal places.
  std::transform(seq.begin(), seq.end(), seq2.begin(),
    std::bind2nd(roundoff<double>(), 2));
    
  std::copy(seq2.begin(), seq2.end(),
            std::ostream_iterator<double>(std::cout, "\n"));
}
