forked from melpon/wandbox
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathForEachAndTransform.cpp
137 lines (115 loc) · 3.39 KB
/
ForEachAndTransform.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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
// This file is a "Insert values" in C++ language by GCC for wandbox.
#include <iostream>
#include <algorithm>
#include <functional>
#include <vector>
#include <cassert>
#include <ranges>
template<class T>
class Compact
{
private:
std::vector<T> vecVals;
public:
Compact()=default;
template<typename... U>
void insertFunc(U&&... v)
{
auto pushVals = [&](auto&& x){ vecVals.push_back(std::forward<decltype(x)>(x));};
(..., pushVals(std::forward<U>(v)));
}
std::vector<T> getCompactVect(void)
{
std::vector<T> lclVec;
int vSize = vecVals.size();
std::cout << "What is the size: " <<vecVals.size()<<std::endl;
for(int x=0; x < vSize; x++)
{
// std::cout << "What is the vecVals[x]: " <<vecVals[x]<<std::endl;
lclVec.push_back(vecVals[x]);
}
return(lclVec);
}
void printOut()
{
for(auto z : vecVals)
std::cout<<z<<"\n";
}
};
//pointer Specialisation
template<class T>
class Compact<T*>
{
private:
std::vector<T*> vecVals;
public:
Compact()=default;
template<typename... U>
void insertFunc(U*&&... v)
{
auto pushVals = [&](auto&& x){ vecVals.push_back(std::forward<decltype(x)>(x));};
(..., pushVals(std::forward<U*>(v)));
}
std::vector<T> getCompactVect(void)
{
std::vector<T> lclVec;
int vSize = vecVals.size();;
for(int x=0; x<vSize; x++)
{
// std::cout << "What is the *vecVals[x]: " <<*vecVals[x]<<std::endl;
lclVec.push_back(*vecVals[x]);
}
return(lclVec);
}
void printOut()
{
for(auto z : vecVals)
std::cout<<*z<<"\n";
}
};
int main()
{
std::cout << "Hello, time to insert!\n" << std::endl;
Compact<int> mainComp;
Compact<int*> ptrComp;
std::vector<int> finalCompVect;
std::vector<int> forCompVect;
int a = -1, b = -2, c = -3, d = -4, e = -5, f= -6, g = -7, h = -8, i = -9;
mainComp.insertFunc(a,b,c,d,e,f,g,h,i);
ptrComp.insertFunc(&a,&b,&c,&d,&e,&f,&g,&h,&i);
mainComp.printOut();
std::cout << "\n\nHello, insert from pointer!\n" << std::endl;
ptrComp.printOut();
std::vector<int> mainCompVect = mainComp.getCompactVect();
std::vector<int> ptrCompVect = ptrComp.getCompactVect();
std::cout << "\n\nOutput contents 1!\n" << std::endl;
for(auto z : mainCompVect)
{
std::cout<<z<<"\n";
}
std::cout << "\n\nOutput contents 2!\n" << std::endl;
for(auto z : ptrCompVect)
{
std::cout<<z<<"\n";
}
finalCompVect.resize(ptrCompVect.size());
forCompVect.resize(mainCompVect.size());
std::transform(mainCompVect.begin(), mainCompVect.end(), ptrCompVect.begin(), finalCompVect.begin(), std::multiplies<int>());
std::for_each(ptrCompVect.begin(), ptrCompVect.end(), [](int& x)mutable{x*=x;});
std::cout << "\n\nDot product of both vectors is \n" << std::endl;
for(auto z : finalCompVect)
{
std::cout<<z<<"\n";
}
std::cout << "\n\noutput from mainCompVect is \n" << std::endl;
for(auto z : mainCompVect)
{
std::cout<<z<<"\n";
}
std::cout << "\n\noutput from ptrCompVect is \n" << std::endl;
for(auto z : ptrCompVect)
{
std::cout<<z<<"\n";
}
return 0;
}