File: make_array.md

package info (click to toggle)
jsoncons 1.3.2-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 17,584 kB
  • sloc: cpp: 136,382; sh: 33; makefile: 5
file content (94 lines) | stat: -rw-r--r-- 2,166 bytes parent folder | download
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
### jsoncons::basic_json::make_array

```cpp
template <typename T>
static basic_json make_array(size_ n, const T& val)

template <typename T>
static basic_json make_array(size_ n, const T& val, 
                             const allocator_type& alloc = allocator_type())

template <std::size_t N>
static basic_json make_array(std::size_t size1 ... size_t sizeN)

template <std::size_t N,typename T>
static basic_json make_array(std::size_t size1 ... size_t sizeN, const T& val)

template <std::size_t N,typename T>
static basic_json make_array(std::size_t size1 ... size_t sizeN, const T& val, 
                             const allocator_type& alloc)
```
Makes a multidimensional array with the number of dimensions specified as a template parameter. The size of each dimension is passed as a parameter, and optionally an inital value. If no initial value, the default is an empty json object. The elements may be accessed using familiar C++ native array syntax.

### Examples

#### Make an array of size 10 initialized with zeros
```cpp
json a = json::make_array(10,0); // angle brackets can be omitted when N = 1
a[1] = 1;
a[2] = 2;
std::cout << pretty_print(a) << '\n';
```
Output:
```json
[0,1,2,0,0,0,0,0,0,0]
```
#### Make a two dimensional array of size 3x4 initialized with zeros
```cpp
json a = json::make_array<2>(3,4,0);
a[0][0] = "Tenor";
a[0][1] = "ATM vol";
a[0][2] = "25-d-MS";
a[0][3] = "25-d-RR";
a[1][0] = "1Y";
a[1][1] = 0.20;
a[1][2] = 0.009;
a[1][3] = -0.006;
a[2][0] = "2Y";
a[2][1] = 0.18;
a[2][2] = 0.009;
a[2][3] = -0.005;

std::cout << pretty_print(a) << '\n';
```
Output:
```json
[
    ["Tenor","ATM vol","25-d-MS","25-d-RR"],
    ["1Y",0.2,0.009,-0.006],
    ["2Y",0.18,0.009,-0.005]
]
```
#### Make a three dimensional array of size 4x3x2 initialized with zeros
```cpp
json a = json::make_array<3>(4,3,2,0);
a[0][2][0] = 2;
a[0][2][1] = 3;
std::cout << pretty_print(a) << '\n';
```
Output:
```json
[
    [
        [0,0],
        [0,0],
        [2,3]
    ],
    [
        [0,0],
        [0,0],
        [0,0]
    ],
    [
        [0,0],
        [0,0],
        [0,0]
    ],
    [
        [0,0],
        [0,0],
        [0,0]
    ]
]
```