This C++ program demonstrates using binary arithmetic function object. Tbe program creates two vectors with integer elements and adds them using ‘plus’ function object which is passed as a parameter to the transform algorithm which adds the corresponding elements and stores the sum in the third vector.

Here is the source code of the C++ program which demonstrates using binary 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 binary arithmetic function objects`

`*/`

`#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(' ') << a[i] << " + "

<< setw(1) << setfill(' ') << b[i] << " = "

<< setw(1) << setfill(' ') << 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 << "Addition using \'plus\' arithmetic function object"

<< endl;

transform(a.begin(), a.end(), b.begin(), c.begin(), plus <int>());

print(a, b, c);

`}`

$ a.out Addition using 'plus' arithmetic function object a[i] b[i] c[i] 1 + 1 = 2 2 + 2 = 4 3 + 3 = 6 4 + 4 = 8 5 + 1 = 6 1 + 2 = 3 2 + 3 = 5 3 + 4 = 7 4 + 1 = 5 5 + 2 = 7

**Sanfoundry Global Education & Learning Series – 1000 C++ Programs.**

If you wish to look at all C++ Programming examples, go to C++ Programs.