Converting into multidimensional std::array

Viewed 78
int data[][4] = { {1,2,3,4}, {9,8,7,6}, {2,4,6,8} };

I want to convert this into multidimensional std::array

array< array<int,4>, 3 > stddata = { {1,2,3,4}, {9,8,7,6}, {2,4,6,8} };

like this. But error occur in this code. Why does this error occur? and how can I change reset part { {1,2,3,4}, {9,8,7,6}, {2,4,6,8} } to { , } numbers.

1 Answers

Update: Here is code using std::array

#include <iostream>
#include <array>

int main() {
    // your code goes here
    std::array< std::array<int, 4>, 3> stddata = {{{1,2,3,4}, {9,8,7,6}, {2,4,6,8}}};
    return 0;
}

If the usecase is to add / remove more values during the lifetime of the container, we can use std::vector. Here is sample code:

#include <iostream>
#include <vector>

int main() {
    // your code goes here
    std::vector< std::vector<int>> stddata = { {1,2,3,4}, {9,8,7,6}, {2,4,6,8} };
    return 0;
}
Related