C++ Program to Demonstrate using Multiplies Function Object
#include <iostream>
#include <algorithm>
#include <vector>
#include <functional>
#include <iterator>
#include <iomanip>
using namespace std;
typedef const vector <int>& vecref;
void print(vecref a, vecref b, vecref c)
{
cout << "a[i] b[i] c[i]" << endl;
for(int i = 0; i < a.size(); i++)
{
cout << setw(2) << setfill('0') << a[i] << " * "
<< setw(2) << setfill('0') << b[i] << " = "
<< setw(2) << setfill('0') << c[i] << endl;
}
}
int main()
{
vector <int> a(10), b(10), c(10);
for (int i = 0; i < 10 ;i++)
{
a[i] = (i % 5 + 1);
b[i] = (i % 4 + 1);
}
// Save the result in vector c
cout << "Multiplication using \'multiplies\' arithmetic function object"
<< endl;
transform(a.begin(), a.end(), b.begin(), c.begin(), multiplies <int>());
print(a, b, c);
}
Output:
Multiplication using 'multiplies' arithmetic function object
a[i] b[i] c[i]
01 * 01 = 01
02 * 02 = 04
03 * 03 = 09
04 * 04 = 16
05 * 01 = 05
01 * 02 = 02
02 * 03 = 06
03 * 04 = 12
04 * 01 = 04
05 * 02 = 10
