-
Notifications
You must be signed in to change notification settings - Fork 26
/
Copy path21.5 BinaryFunctionMultiplyRanges.cpp
49 lines (40 loc) · 1.45 KB
/
21.5 BinaryFunctionMultiplyRanges.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
#include <vector>
#include <iostream>
#include <algorithm>
template <typename elementType>
class Multiply
{
public:
elementType operator() (const elementType& elem1,
const elementType& elem2)
{
return(elem1 * elem2);
}
};
int main()
{
using namespace std;
vector<int> vecMultiplicand{ 0, 1, 2, 3, 4 };
vector<int> vecMultiplier{ 100, 101, 102, 103, 104 };
// A third container that holds the result of multiplication
vector<int> vecResult;
// Make space for the result of the multiplication
vecResult.resize(vecMultiplier.size());
transform(vecMultiplicand.begin(), // range of multiplicands
vecMultiplicand.end(), // end of range
vecMultiplier.begin(), // multiplier values
vecResult.begin(), // range that holds result
Multiply<int>() ); // the function that multiplies
cout << "The contents of the first vector are: " << endl;
for(size_t index = 0; index < vecMultiplicand.size(); ++ index)
cout << vecMultiplicand [index] << ' ';
cout << endl;
cout << "The contents of the second vector are: " << endl;
for(size_t index = 0; index < vecMultiplier.size(); ++index)
cout << vecMultiplier [index] << ' ';
cout << endl;
cout << "The result of the multiplication is: " << endl;
for(size_t index = 0; index < vecResult.size(); ++ index)
cout << vecResult [index] << ' ';
return 0;
}