This C++ program demonstrates using multiplies arithmetic function object. Tbe program creates two vectors with integer elements and multiplies them using ‘multiplies’ function object which is passed as a parameter to the transform algorithm which multiplies the corresponding elements and stores the product in the third vector.
Here is the source code of the C++ program which demonstrates using multiplies arithmetic function object. The C++ program is successfully compiled and run on a Linux system. The program output is also shown below.
/*
* 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);
}
$ a.out 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
Sanfoundry Global Education & Learning Series – 1000 C++ Programs.
advertisement
advertisement
If you wish to look at all C++ Programming examples, go to C++ Programs.
If you find any mistake above, kindly email to [email protected]Related Posts:
- Practice Programming MCQs
- Check Programming Books
- Apply for Computer Science Internship
- Apply for C++ Internship
- Check Computer Science Books