Search code examples
c++arraysconstructorinitializationdefault

C++ More Elegant Way to set Default Values in multi-dimensional array


In my .h file, I have:

struct tup{
    tup() : 
    token{{-1,"a","b","c","d","e","f"},
          {-1,"a","b","c","d","e","f"},
          ...
          {-1,"a","b","c","d","e","f"}} {}
    struct {
        int pos;
        std::string nj, ny, pa, ri, ct, fl;
    } token[100];

Where the "..." refers to 97 more lines of the same type of code. Is there a more elegant way to set default values for my token?


Solution

  • If you are open to using std::vector instead of an array, you an use:

    struct tup{
        tup() : tokens(100, {-1,"a","b","c","d","e","f"}) {}
        struct token {
            int pos;
            std::string nj, ny, pa, ri, ct, fl;
        };
        std::vector<token> tokens;
    };