-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathArray.cpp
More file actions
431 lines (319 loc) · 11.7 KB
/
Array.cpp
File metadata and controls
431 lines (319 loc) · 11.7 KB
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
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
// =====================================================================================
// Array.cpp // std::array // std::to_array // std::span
// =====================================================================================
module modern_cpp:class_array;
namespace StdArray {
// -------------------------------------------------------------------
// initialization
static void test_01() {
// initialization variants
[[maybe_unused]] std::array<int, 5> array1;
std::array<int, 5> array2{};
std::array<int, 5> array3{ 1, 2, 3, 4, 5 }; // brace initialization
std::array<int, 5> array4 = { 1, 2, 3, 4, 5 }; // initializer list
std::array array5 = { 1, 2, 3, 4, 5 }; // CTAD - type is deduced to std::array<int, 5>
}
// -------------------------------------------------------------------
// assignment
static void test_02() {
std::array<int, 5> array;
array = { 1, 2, 3, 4, 5 }; // assignment with initializer list: okay
array = { 9, 8, 7 }; // Okay, elements 3 and 4 are set to zero!
// array = { 0, 1, 2, 3, 4, 5 }; // doesn't compile, too many elements in initializer list!
}
// -------------------------------------------------------------------
// exception handling
static void test_03() {
// Exception handling:
// 'at' does bounds checking, is therefore slower - but safer.
// subscript operator [] does not do any bounds-checking:
// If an invalid index is provided, unexpected behaviour will happen.
// use subscript operator to access array, works like you would expect
std::array<int, 5> array { 1, 2, 3, 4, 5 };
std::println("{}", array[3]);
// undefined behaviour
//std::println("{}", array[5]);
// valid index
array.at(2) = 33;
// invalid index
try {
array.at(9) = 10;
}
catch (const std::out_of_range&)
{
std::println("Wrong index used!");
}
for (auto elem : array) {
std::print("{} ", elem);
}
std::println();
}
// -------------------------------------------------------------------
// passing std::array as parameter
static void print(const std::array<int, 5>& array) {
std::println("Length: {}", array.size());
}
template<typename T, int Length>
void print(const std::array<T, Length>& array) {
std::println("Length: {}", array.size());
}
static void test_04() {
std::array<int, 5> array1 = { 1, 2, 3, 4, 5 };
print(array1);
std::array<int, 10> array2 = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
print(array2);
}
// -------------------------------------------------------------------
// miscellaneous
static void test_05() {
std::array<int, 5> array { 1, 2, 3, 4, 5 };
for (auto elem : array) {
std::println("{}", elem);
}
std::println("front:{}", array.front());
std::println("back: {}", array.back());
array.fill(123);
for (auto elem : array) {
std::println("{}", elem);
}
std::println();
}
// -------------------------------------------------------------------
// multidimensional arrays
static void test_06() {
// multidimensional std::array
std::array<std::array<int, 3>, 3> array {
{
{ 1, 2, 3 },
{ 4, 5, 6 },
{ 7, 8, 9 }
}
};
for (std::size_t i{}; i != array.size(); i++) {
for (std::size_t j{}; j != array[0].size(); j++) {
std::print("{} ", array[i][j]);
}
std::println();
}
}
static void display(const std::array<std::array<int, 3>, 3>& array) {
for (std::size_t i{}; i != 3; i++) {
for (std::size_t j{}; j != 3; j++) {
std::print("{} ", array[i][j]);
}
std::println();
}
}
static void test_07() {
// passing a multidimensional std::array to another function
std::array<std::array<int, 3>, 3> array
{
{
{ 11, 12, 13 },
{ 14, 15, 16 },
{ 17, 18, 19 }
}
};
display(array);
}
// -------------------------------------------------------------------
// arrays of objects
class Employee {
public:
std::size_t m_id;
std::string m_name;
std::string m_role;
std::size_t m_phone;
Employee() : Employee(0, "", "", 0) {}
Employee(std::size_t m_id, const std::string& m_name, const std::string& m_role, std::size_t m_phone)
: m_id{ m_id }, m_name{ m_name }, m_role{ m_role }, m_phone{ m_phone }
{}
};
static void test_08() {
// arrays of objects
std::array<Employee, 2> employees
{
Employee{ 12345, "Sepp", "Engineer", 987654321 },
Employee{ 54321, "Hans", "Manager", 123456789 }
};
for (const auto& [id, name, role, phone] : employees)
{
std::println("Id: {}", id);
std::println("Name: {}", name);
std::println("Role: {}", role);
std::println("Phone: {}", phone);
}
}
// -------------------------------------------------------------------
// assignment - comparison C-style array with std::string
static void test_09() {
/* C-style array
*/
std::string cArray[4] = { "the", "quick", "brown", "fox" };
// copying via operator= isn't supported:
// array type 'std::string [4]' is not assignable
std::string other[4];
// other = cArray;
// algorithm std::copy works
std::copy(
std::begin(cArray),
std::end(cArray),
std::begin(other)
);
#pragma warning(push)
#pragma warning(disable : 5056)
// Worse: operator== compiles, but does the "wrong" thing: address comparison!
bool isEqual = (cArray == other);
#pragma warning(pop)
// works as expected - need to use a standard algorithm
isEqual = std::equal(
std::begin(cArray),
std::end(cArray),
std::begin(other),
std::end(other)
);
}
static void test_10() {
/* std::array
*/
std::array<std::string, 4> array{ "the", "quick", "brown", "fox" };
// copying via operator= is supported ... in linear time
std::array<std::string, 4> other;
other = array;
// operator== compiles and does expected value comparison of all array elements!
bool isEqual = (array == other);
}
// -------------------------------------------------------------------
// returning an array - comparison C-style array with std::string
// returning a C-style
#pragma warning(push)
#pragma warning(disable : 4172)
// returning address of local variable or temporary C style array:
// compiles, but is false
static int* scalarProduct(int scalar, const int vec[3])
{
int result[3] = { scalar * vec[0], scalar * vec[1], scalar * vec[2] };
return result;
}
#pragma warning(pop)
// returning an std::array
static std::array<int, 3> scalarProduct(int scalar, const std::array<int, 3>& vec)
{
std::array result = { scalar * vec[0], scalar * vec[1], scalar * vec[2] };
return result;
}
static void test_11()
{
// testing scalarProduct with C-style array
int vector[3] { 1, 2, 3 };
int* result{ scalarProduct(3, vector) };
std::println("Result: {},{},{}", result[0], result[1], result[2]);
// results can be correct, but that is absolutely coincidental
// testing scalarProduct with std::array
std::array vector2 { 1, 2, 3 };
std::array result2{ scalarProduct(3, vector2) };
std::println("Result: {},{},{}", result2[0], result2[1], result2[2]);
}
// -------------------------------------------------------------------
// std::to_array
// helper for creating a std::array from a C-array
static void test_20() {
// type is deduced to std::array<char, 13>
// Note: see std::array above
auto array1{ std::to_array("Hello C++ 20") };
// type is deduced to std::array<char, 13>
// Note: see std::array above
auto array2{ std::to_array<const char>("Hello C++ 20") };
// type is deduced to std::array<int, 5>
auto array3{ std::to_array({ 1, 2, 3, 4, 5 }) };
// type is deduced to std::array<long, 5>
auto array4{ std::to_array<long>({ 1, 2, 3, 4, 5 }) };
// type is deduced to std::array<int, 5>
int intNumbers[] { 1, 2, 3, 4, 5 };
auto array5{ std::to_array(intNumbers) };
}
// -------------------------------------------------------------------
// std::span
// demonstrating printArray with pointer parameter
static void printArray(const int* array, std::size_t size) {
std::println("Number of elements: {}", size);
// note: range-based loop doesn't work for pointers
for (std::size_t i{}; i != size; ++i) {
std::println("{} ", array[i]);
}
std::println();
}
static void test_30() {
int carr[]{ 1, 2, 3, 4, 5 };
printArray(carr, 5);
std::array arr{ 6, 7, 8, 9, 10 };
printArray(arr.data(), arr.size());
std::vector vec{ 1, 3, 5, 7, 9 };
printArray(vec.data(), vec.size());
}
// --------------------------------------------------------------------
// demonstrating printArray with std::span parameter
static void printArray(std::span<int> values) {
std::println("Number of elements: {}", values.size());
std::println("Size of span: {}", values.size_bytes());
// range-based loop works now
for (auto elem : values) {
std::println("{}", elem);
}
std::println();
}
static void test_31() {
//int carr[]{ 1, 2, 3, 4, 5 };
//printArray(carr);
//std::array arr{ 6, 7, 8, 9, 10 };
//printArray(arr);
//std::vector<int> vec{ 1, 3, 5, 7, 9 };
//printArray(vec);
int carr[]{ 1, 2, 3, 4, 5 };
printArray(std::span{ carr });
std::array arr{ 6, 7, 8, 9, 10 };
printArray(std::span{ arr });
std::vector vec{ 1, 3, 5, 7, 9 };
printArray(std::span{ vec });
}
// --------------------------------------------------------------------
// demonstrating std::span with const type
static void printArrayConst(std::span<const int> values) {
std::println("Number of elements: {}", values.size());
std::println("Size of span: {}", values.size_bytes());
for (const auto elem : values) {
std::println("{}", elem);
}
std::println();
}
static void test_32() {
int carr[]{ 1, 2, 3, 4, 5 };
printArrayConst(carr);
std::array arr{ 6, 7, 8, 9, 10 };
printArrayConst(arr);
std::vector vec{ 1, 3, 5, 7, 9 };
printArrayConst(vec);
}
}
void main_array()
{
using namespace StdArray;
test_01();
test_02();
test_03();
test_04();
test_05();
test_06();
test_07();
test_08();
test_09();
test_10();
test_11();
test_20();
test_30();
test_31();
test_32();
}
// =====================================================================================
// End-of-File
// =====================================================================================