Search code examples
c++vectorstructcell-array

C++ Store multi-sized arrays in a variable


I am trying to store a cell-like structure in C++, where its elements can have arrays of different lengths like the following example:

myMultiSizedArray = { 
      { 1, 2, 4 },
      { 3, 5, 6, 7 },
      { 7, 8, 9, 10 },
      { 1, 3 },
      { 4, 5, 8 },
      { 9, 10 } 
      { 5 } }

I am thinking of using a vector in a struct such as the following:

struct f
{
    std::vector<int> elements;
};

std::vector<f> myMultiSizedArray;

I would appreciate it if the community could give me their feedback. Are there better, more efficient approaches? Does C++ provide a means for this? Thank you


Solution

  • As mentioned by other users as comment, you could use a vector inside another vector as in piece of code below:


    using namespace std;
    vector<vector<int>> myMultiSizedArray;
    
    myMultiSizedArray.push_back({ 1, 2, 3, 4 });
    myMultiSizedArray.push_back({ 6, 5, 200, 3, 2, 1 });