This C++ program illustrates the transform() algorithm. The program creates two vectors and transforms the third vector by inserting a value equal to an element from first vector raise to the power of element in second vector. The function power is passed as a predicate to the function transform().
Here is the source code of the C++ program which illustrates the transform() algorithm. The C++ program is successfully compiled and run on a Linux system. The program output is also shown below.
/** C++ Program to illustrate transform algorithm*/#include <iostream>#include <algorithm>#include <vector>#include <functional>#include <iterator>#include <iomanip>#include <cmath>using namespace std;
typedef const vector <int>& vecref;
int power(int a, int b)
{return pow(a, b);
}void print(vecref a, vecref b, vecref c)
{cout << "b[i] a[i] c[i]" << endl;
for(int i = 0; i < a.size(); i++)
{cout << setw(2) << setfill(' ') << a[i] << " ^ "
<< setw(1) << setfill(' ') << b[i] << " = "
<< setw(2) << setfill(' ') << c[i] << endl;
}}int main()
{vector <int> a(10), b(10), c(10);
for (int i = 0; i < 10 ;i++)
{a[i] = (i % 2 + 1);
b[i] = (i % 3 + 1);
}// Save the result in vector ccout << "Transform operation" << endl;
transform(b.begin(), b.end(), a.begin(), c.begin(), power);
print(b, a, c);
}
$ a.out Transform operation b[i] a[i] c[i] 1 ^ 1 = 1 2 ^ 2 = 4 3 ^ 1 = 3 1 ^ 2 = 1 2 ^ 1 = 2 3 ^ 2 = 9 1 ^ 1 = 1 2 ^ 2 = 4 3 ^ 1 = 3 1 ^ 2 = 1
Sanfoundry Global Education & Learning Series – 1000 C++ Programs.
advertisement
If you wish to look at all C++ Programming examples, go to C++ Programs.
Related Posts:
- Practice Computer Science MCQs
- Apply for Computer Science Internship
- Check Computer Science Books
- Apply for C++ Internship
- Practice Programming MCQs