-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMultiArray2d.cpp
More file actions
55 lines (44 loc) · 1.5 KB
/
MultiArray2d.cpp
File metadata and controls
55 lines (44 loc) · 1.5 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
/**
* \file MultiArray2d.cpp
* \brief
*/
#include <StdStream/StdStream.h>
#include <StdTest/StdTest.h>
#include <Stl.h>
#include <boost/multi_array.hpp>
//--------------------------------------------------------------------------------------------------
// safety: array dimensions and data tied into one, can't mix them up!
// also: type safety.
void
print(const boost::multi_array<double, 2> &array)
{
for (std::size_t i = 0; i < array.shape()[0]; i++) {
for (std::size_t j = 0; j < array.shape()[1]; j++) {
std::cout << array[i][j] << " ";
}
std::cout << "\n";
}
std::cout << "\n";
}
//--------------------------------------------------------------------------------------------------
int main(int argc, char **argv)
{
const std::size_t m = (argc > 1) ? atoi(argv[1]) : 4;
const std::size_t n = (argc > 2) ? atoi(argv[2]) : 5;
boost::multi_array<double, 2> A(boost::extents[n][m]);
for (std::size_t i = 0; i < n; i++)
for (std::size_t j = 0; j < m; j++)
A[i][j] = i * m + j;
boost::multi_array<double, 2> B(boost::extents[n][m]);
for (std::size_t i = 0; i < n; i++)
for (std::size_t j = 0; j < m; j++)
B[i][j] = i * m + j + 0.5;
print(A);
print(B);
// safe copies, would even work with differing dimensions:
B = A;
print(B);
// RAII: no manual free, no memory leaks
return 0;
}
//--------------------------------------------------------------------------------------------------