Skip to content

Commit 557d551

Browse files
committed
stl's 'trasform' algorithm for vectors
1 parent b55f010 commit 557d551

File tree

1 file changed

+55
-0
lines changed

1 file changed

+55
-0
lines changed

vectors/transform_une.cpp

+55
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
#include <iostream>
2+
#include <vector>
3+
4+
#include <algorithm> // trasnform function
5+
6+
using namespace std;
7+
8+
template<class T>
9+
T SQRT(T num) {
10+
return (num<=0) ? 0 : num*num;
11+
}
12+
13+
template<class T>
14+
T CUBE(T num) {
15+
return (num<=0) ? 0 : num*num*num;
16+
}
17+
18+
template<class T>
19+
void PRINT(const vector<T> &vec) {
20+
for(const auto & v : vec)
21+
cout << v << " ";
22+
cout << endl;
23+
}
24+
25+
template<class type>
26+
void Transform(vector<type> &vec, size_t operation = 0) {
27+
switch(operation)
28+
{
29+
case 0:
30+
transform(vec.begin(), vec.end(), vec.begin(), SQRT<type>);
31+
PRINT<type>(vec);
32+
break;
33+
case 1:
34+
transform(vec.begin(), vec.end(), vec.begin(), CUBE<type>);
35+
PRINT<type>(vec);
36+
break;
37+
default:
38+
cout << "Choose square root OR cube operations only...\n";
39+
break;
40+
}
41+
}
42+
43+
int main()
44+
{
45+
vector<int> nums{1,2,3,4,5,6,7,8,9,10};
46+
cout << "Nums vector:\n";
47+
PRINT<int>(nums);
48+
cout << "Nums vector squared:\n";
49+
Transform<int>(nums);
50+
cout << "Nums vector squared-squared:\n";
51+
Transform<int>(nums, 1);
52+
53+
54+
return 0;
55+
}

0 commit comments

Comments
 (0)