Ne.app NeANT
The Ne.app NeANT User module of the NeANT distribution.
slice_array.cpp
Go to the documentation of this file.
1
29#include <iostream> // for cout, endl
30#include <valarray.h> // Includes valarray and provides stream inserter.
31#include <examples.h> // Generic header for all examples.
32
33typedef std::valarray<int> valarray_t;
34
35int main(void) {
36
37 valarray_t::value_type ibuf[9] = {0,1,2,3,4,5,6,7,8};
38
39 // Create a valarray initialized to the sequence of integers above.
40 valarray_t vi (ibuf,9);
41
42 std::cout << "original valarray vi\n\n" << vi << "\n\n";
43
44 // Display a slice of the valarray.
45 std::cout << "vi[slice(0,3,3)]\n\n" << vi[std::slice(0,3,3)] << "\n\n";
46
47 // Unfortunately, slice_arrays were not designed to be easily combined.
48 // An explicit conversion to valarray is required, leaving us with
49 // this awkward casting syntax.
50 vi[std::slice (0,3,3)] = static_cast<valarray_t>(vi[std::slice (1,3,3)]) +
51 static_cast<valarray_t>(vi[std::slice (2,3,3)]);
52
53 // The operation above uses slices ([0-2],3,3) to treat our valarray
54 // as a 3-dimensional array:
55 //
56 // 0 1 2 3 = 1 + 2
57 // 3 4 5 9 = 4 + 5
58 // 6 7 8 15 = 7 + 8
59 // | | | | | |
60 // | | --slice(2,3,3) | | -slice_array for arithmetic operand.
61 // | ----slice(1,3,3) | -----slice_array for arithmetic operand.
62 // ------slice(0,3,3) ----slice_array assignment refers to valarray.
63
64 std::cout << "vi[slice(0,3,3)] = vi[slice(1,3,3)] + vi[slice(2,3,3)]\n\n"
65 << vi << std::endl;
66
67 return 0;
68}
std::valarray< int > valarray_t
Definition gslice.cpp:33
int main(void)
std::valarray< int > valarray_t