-
Notifications
You must be signed in to change notification settings - Fork 1
/
templates_by_reference.hpp
332 lines (271 loc) · 9.86 KB
/
templates_by_reference.hpp
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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
#ifndef __TEMPLATES_BY_REFERENCE_H
#define __TEMPLATES_BY_REFERENCE_H
#include <iostream>
#include <iomanip>
#include <fstream>
#include <sstream>
#include <string>
#include <vector>
#include <deque>
#include <list>
#include <stack>
#include <queue>
#include <set>
#include <map>
#include <unordered_set>
#include <unordered_map>
#include <algorithm>
#include <numeric>
#include <functional>
#include <iterator>
#include <stdexcept>
#include <exception>
#include <limits>
#include <random>
#include <typeinfo>
#include <tuple>
#include <regex>
#include <memory>
#include <atomic>
#include <thread>
#include <mutex>
#include <condition_variable>
#include <future>
#include <chrono>
#include <filesystem>
#include <chrono>
#include <optional>
#include <cassert>
#include <cstddef>
#include <cwctype>
#include <ctime>
#include <cstdlib>
#include <cstdint>
#include <climits>
#include <cctype>
#include <cstring>
#include <cmath>
using namespace std;
/*
- Templates By Reference:
- Passing template parameters by reference is a powerful technique that offers efficiency and flexibility, but it also requires careful consideration of object lifetimes and potential modifications.
- It's commonly used when you want to work with and potentially modify existing objects efficiently within a template algorithm or function.
- When you pass a template parameter by reference, you are allowing the function or class template to work with the original object, rather than creating a copy.
- This can have implications for performance, memory usage, and the ability to modify the original object.
*/
#include <iostream>
#include <vector>
// Using the provided naming conventions
namespace cpp_func_templates {
template <typename Container>
double cpp_func_modifyAndCalculateAverage(Container& container) {
if (container.empty()) {
throw std::invalid_argument("Container must not be empty");
}
double sum = 0.0;
for (auto& element : container) {
// Modify the element in some way (e.g., increment by 1)
element += 1;
sum += static_cast<double>(element);
}
return sum / container.size();
}
template <typename Container>
void cpp_func_elementWiseAdd(const Container& container1, const Container& container2, Container& result) {
if (container1.size() != container2.size() || container1.size() != result.size()) {
throw std::invalid_argument("Containers must have the same size");
}
auto it1 = container1.begin();
auto it2 = container2.begin();
auto itResult = result.begin();
while (it1 != container1.end()) {
*itResult = *it1 + *it2;
++it1;
++it2;
++itResult;
}
}
template <typename Container>
void cpp_func_findCommonElements(const Container& container1, const Container& container2, Container& result) {
std::set_intersection(container1.begin(), container1.end(),
container2.begin(), container2.end(),
std::back_inserter(result));
}
template <typename Container, typename Transformation>
void processData(Container& data, Transformation transform) {
std::transform(data.begin(), data.end(), data.begin(), transform);
}
template <typename Container, typename Comparator>
void customSort(Container& container, Comparator comp) {
std::sort(container.begin(), container.end(), comp);
}
template <typename Node>
void traverseLinkedList(Node* head) {
while (head != nullptr) {
std::cout << head->data << " ";
head = head->next;
}
std::cout << std::endl;
}
template <typename Container>
void printContainer(const Container& container) {
for (const auto& element : container) {
std::cout << element << " ";
}
std::cout << std::endl;
}
template <typename T>
void matrixAddition(const std::vector<std::vector<T>>& matrix1,
const std::vector<std::vector<T>>& matrix2,
std::vector<std::vector<T>>& result) {
// Perform matrix addition
for (size_t i = 0; i < matrix1.size(); ++i) {
for (size_t j = 0; j < matrix1[i].size(); ++j) {
result[i][j] = matrix1[i][j] + matrix2[i][j];
}
}
}
#include <fstream>
template <typename T>
void serializeData(const T& data, std::ofstream& outputFile) {
// Serialize and write data to the file
outputFile.write(reinterpret_cast<const char*>(&data), sizeof(data));
}
template <typename T>
void deserializeData(T& data, std::ifstream& inputFile) {
// Read and deserialize data from the file
inputFile.read(reinterpret_cast<char*>(&data), sizeof(data));
}
}
struct ListNode {
int data;
ListNode* next;
};
void cpp_func_templates_call() {
// cpp_func_templates::cpp_func_modifyAndCalculateAverage
{
std::vector<int> intVector = { 10, 20, 30, 40, 50 };
try {
double average = cpp_func_templates::cpp_func_modifyAndCalculateAverage(intVector);
std::cout << "Modified elements in intVector and calculated average: " << average << std::endl;
std::cout << "Modified intVector: ";
for (const auto& element : intVector) {
std::cout << element << " ";
}
std::cout << std::endl;
}
catch (const std::exception& ex) {
std::cerr << "Error: " << ex.what() << std::endl;
}
}
// cpp_func_templates::cpp_func_elementWiseAdd
{
std::vector<int> intVector1 = { 1, 2, 3, 4, 5 };
std::vector<int> intVector2 = { 5, 4, 3, 2, 1 };
std::vector<int> intResult(5);
try {
cpp_func_templates::cpp_func_elementWiseAdd(intVector1, intVector2, intResult);
std::cout << "Element-wise addition result: ";
for (const auto& element : intResult) {
std::cout << element << " ";
}
std::cout << std::endl;
}
catch (const std::exception& ex) {
std::cerr << "Error: " << ex.what() << std::endl;
}
}
// cpp_func_templates::cpp_func_findCommonElements
{
std::vector<int> intVector1 = { 1, 2, 3, 4, 5 };
std::vector<int> intVector2 = { 3, 4, 5, 6, 7 };
std::vector<int> commonElements;
try {
cpp_func_templates::cpp_func_findCommonElements(intVector1, intVector2, commonElements);
std::cout << "Common elements between intVector1 and intVector2: ";
for (const auto& element : commonElements) {
std::cout << element << " ";
}
std::cout << std::endl;
}
catch (const std::exception& ex) {
std::cerr << "Error: " << ex.what() << std::endl;
}
}
// cpp_func_templates::processData
{
std::vector<int> intVector = { 1, 2, 3, 4, 5 };
cpp_func_templates::processData(intVector, [](int value) {
return value * value; // Square each element
});
std::cout << "Processed intVector: ";
for (const auto& element : intVector) {
std::cout << element << " ";
}
std::cout << std::endl;
}
// cpp_func_templates::customSort
{
std::vector<int> intVector = { 5, 3, 8, 1, 2, 7 };
cpp_func_templates::customSort(intVector, [](int a, int b) {
return a > b; // Sort in descending order
});
std::cout << "Sorted intVector: ";
for (const auto& element : intVector) {
std::cout << element << " ";
}
std::cout << std::endl;
}
// cpp_func_templates::traverseLinkedList
{
ListNode* node1 = new ListNode{1, nullptr};
ListNode* node2 = new ListNode{2, nullptr};
ListNode* node3 = new ListNode{3, nullptr};
node1->next = node2;
node2->next = node3;
cpp_func_templates::traverseLinkedList(node1);
delete node1;
delete node2;
delete node3;
}
// cpp_func_templates::printContainer
{
std::vector<int> intVector = { 1, 2, 3, 4, 5 };
std::vector<double> doubleVector = { 3.14, 2.71, 1.618 };
cpp_func_templates::printContainer(intVector);
cpp_func_templates::printContainer(doubleVector);
}
// cpp_func_templates::matrixAddition
{
std::vector<std::vector<int>> matrix1 = { {1, 2}, {3, 4} };
std::vector<std::vector<int>> matrix2 = { {5, 6}, {7, 8} };
std::vector<std::vector<int>> result(2, std::vector<int>(2, 0));
cpp_func_templates::matrixAddition(matrix1, matrix2, result);
std::cout << "Matrix addition result:" << std::endl;
for (const auto& row : result) {
for (const auto& element : row) {
std::cout << element << " ";
}
std::cout << std::endl;
}
}
// cpp_func_templates::serializeData
// cpp_func_templates::deserializeData
{
int intValue = 42;
double doubleValue = 3.14;
std::ofstream output("data.bin", std::ios::binary);
cpp_func_templates::serializeData(intValue, output);
cpp_func_templates::serializeData(doubleValue, output);
output.close();
int readIntValue;
double readDoubleValue;
std::ifstream input("data.bin", std::ios::binary);
cpp_func_templates::deserializeData(readIntValue, input);
cpp_func_templates::deserializeData(readDoubleValue, input);
input.close();
std::cout << "Read int value: " << readIntValue << std::endl;
std::cout << "Read double value: " << readDoubleValue << std::endl;
}
}
#endif