Search code examples
c++structb-tree

Dynamic array length in struct


What I want to do is make a struct that has an array of another struct, and I want to set the size of the array at runtime. Is there any way to do that? Struct is like:

struct MyStruct
{
  AnotherStruct list[];
  int key;
  bool isLeaf;
}

Solution

  • One way is:

    #include <vector>
    
    struct MyStruct
    {
        std::vector<AnotherStruct> list;
        int key;
        bool isLeaf;
    };
    

    If you are new to vectors you can read about how to use them in any C++ reference.