struct array association?

Im not sure how to describe what I need,

I need a structs constructor to have an int passed in and from that int have a predetermined, full, array be the only variable for the struct. all of the arrays are different dimensions, whats the best way to go about this.

Here are the arrays in the file i have right now.

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
  #ifndef GALAGACLASSES_TYPE_H
  #define GALAGACLASSES_TYPE_H


struct Type {
    const char starfighter[17][17] = {'B', 'B', 'B', 'B', 'B', 'B', 'B', 'W', 'B', 'B', 'B', 'B', 'B', 'B', 'B',//1
                                      'B', 'B', 'B', 'B', 'B', 'B', 'B', 'W', 'B', 'B', 'B', 'B', 'B', 'B', 'B',//2
                                      'B', 'B', 'B', 'B', 'B', 'B', 'W', 'W', 'W', 'B', 'B', 'B', 'B', 'B', 'B',//3
                                      'B', 'B', 'B', 'B', 'B', 'B', 'W', 'W', 'W', 'B', 'B', 'B', 'B', 'B', 'B',//4
                                      'B', 'B', 'B', 'B', 'B', 'B', 'W', 'W', 'W', 'B', 'B', 'B', 'B', 'B', 'B',//5
                                      'B', 'B', 'B', 'B', 'R', 'B', 'W', 'W', 'W', 'B', 'R', 'B', 'B', 'B', 'B',//6
                                      'B', 'B', 'B', 'B', 'R', 'B', 'W', 'W', 'W', 'B', 'R', 'B', 'B', 'B', 'B',//7
                                      'B', 'B', 'B', 'B', 'W', 'W', 'W', 'W', 'W', 'B', 'W', 'B', 'B', 'B', 'B',//8
                                      'R', 'B', 'B', 'B', 'Q', 'B', 'W', 'R', 'B', 'B', 'Q', 'B', 'B', 'B', 'R',//8
                                      'R', 'B', 'B', 'Q', 'W', 'B', 'R', 'R', 'R', 'W', 'W', 'Q', 'B', 'B', 'R',//7
                                      'W', 'B', 'B', 'W', 'W', 'B', 'R', 'W', 'R', 'W', 'W', 'W', 'B', 'B', 'W',//6
                                      'W', 'B', 'W', 'W', 'W', 'W', 'W', 'W', 'W', 'W', 'W', 'W', 'W', 'B', 'W',//5
                                      'W', 'W', 'W', 'W', 'W', 'R', 'W', 'W', 'W', 'R', 'W', 'W', 'W', 'W', 'W',//4
                                      'W', 'W', 'W', 'B', 'R', 'R', 'W', 'W', 'W', 'R', 'R', 'B', 'W', 'W', 'W',//3
                                      'W', 'W', 'B', 'B', 'R', 'R', 'B', 'W', 'B', 'R', 'R', 'B', 'B', 'W', 'W',//2
                                      'W', 'B', 'B', 'B', 'B', 'B', 'B', 'W', 'B', 'B', 'B', 'B', 'B', 'B', 'W',};

    const char moth[10][13] = {'B', 'Q', 'B', 'B', 'B', 'B', 'Y', 'B', 'B', 'B', 'B', 'Q', 'B',
                               'B', 'B', 'Q', 'B', 'B', 'R', 'Y', 'R', 'B', 'B', 'Q', 'B', 'B',
                               'B', 'B', 'B', 'Q', 'R', 'R', 'Y', 'R', 'R', 'Q', 'B', 'B', 'B',
                               'B', 'B', 'B', 'B', 'Y', 'Y', 'Y', 'Y', 'Y', 'B', 'B', 'B', 'B',
                               'B', 'B', 'B', 'Q', 'Q', 'Y', 'Y', 'Y', 'Q', 'Q', 'B', 'B', 'B',
                               'B', 'B', 'Q', 'Q', 'Q', 'R', 'R', 'R', 'Q', 'Q', 'Q', 'B', 'B',
                               'B', 'Q', 'Q', 'Q', 'B', 'R', 'R', 'R', 'B', 'Q', 'Q', 'Q', 'B',
                               'Q', 'Q', 'Q', 'Q', 'B', 'Y', 'Y', 'Y', 'B', 'Q', 'Q', 'Q', 'Q',
                               'Q', 'Q', 'Q', 'B', 'B', 'R', 'R', 'R', 'B', 'B', 'Q', 'Q', 'Q',
                               'Q', 'Q', 'Q', 'B', 'B', 'B', 'R', 'B', 'B', 'B', 'Q', 'Q', 'Q'};

    const char galaga[17][15] = {'B', 'B', 'B', 'B', 'B', 'B', 'G', 'B', 'G', 'B', 'B', 'B', 'B', 'B', 'B',
                                 'B', 'B', 'B', 'B', 'B', 'B', 'G', 'B', 'G', 'B', 'B', 'B', 'B', 'B', 'B',
                                 'B', 'B', 'B', 'G', 'G', 'O', 'O', 'G', 'O', 'O', 'G', 'G', 'B', 'B', 'B',
                                 'B', 'B', 'B', 'B', 'G', 'O', 'O', 'G', 'O', 'O', 'G', 'B', 'B', 'B', 'B',
                                 'B', 'B', 'B', 'B', 'B', 'G', 'G', 'G', 'G', 'G', 'B', 'B', 'B', 'B', 'B',
                                 'B', 'B', 'B', 'B', 'B', 'G', 'B', 'G', 'B', 'B', 'B', 'B', 'B', 'B', 'B',
                                 'B', 'B', 'B', 'B', 'G', 'Y', 'Y', 'G', 'Y', 'Y', 'G', 'B', 'B', 'B', 'B',
                                 'B', 'B', 'G', 'G', 'G', 'Y', 'Y', 'Y', 'Y', 'Y', 'G', 'G', 'G', 'B', 'B',
                                 'G', 'G', 'G', 'B', 'B', 'Y', 'Y', 'Y', 'Y', 'Y', 'G', 'G', 'G', 'G', 'G',
                                 'B', 'G', 'G', 'B', 'B', 'Y', 'Y', 'Y', 'Y', 'Y', 'G', 'G', 'G', 'G', 'B',
                                 'B', 'G', 'O', 'G', 'G', 'Y', 'Y', 'Y', 'Y', 'Y', 'G', 'G', 'O', 'G', 'B',
                                 'G', 'G', 'O', 'G', 'B', 'B', 'O', 'B', 'O', 'B', 'B', 'G', 'O', 'G', 'G',
                                 'G', 'O', 'G', 'G', 'B', 'B', 'O', 'B', 'O', 'B', 'B', 'G', 'G', 'O', 'G',
                                 'G', 'O', 'O', 'G', 'B', 'B', 'B', 'B', 'B', 'B', 'B', 'G', 'O', 'O', 'G',
                                 'G', 'O', 'O', 'G', 'B', 'B', 'B', 'B', 'B', 'B', 'B', 'G', 'O', 'O', 'G',
                                 'G', 'G', 'G', 'G', 'B', 'B', 'B', 'B', 'B', 'B', 'B', 'G', 'G', 'G', 'G',
                                 'B', 'G', 'G', 'B', 'B', 'B', 'B', 'B', 'B', 'B', 'B', 'B', 'G', 'G', 'B',};


    const char bullet_shape[8][3] = {'B', 'Q', 'B',
                                     'B', 'Q', 'B',
                                     'Q', 'Q', 'Q',
                                     'Q', 'W', 'Q',
                                     'B', 'R', 'B',
                                     'B', 'R', 'B',
                                     'B', 'R', 'B',
                                     'B', 'R', 'B',};


};

#endif //GALAGACLASSES_TYPE_H 
array be the only variable for the struct

Can you please clarify?
- Do you want only one variable name inside ‘Type’? If so, you don’t need a struct.
- Do you need only one array out of those three and you want to choose it at run time? Perhaps you'd better read your data from a database (a simple text file could do the trick) (example 1 below).
- Do you want your struct to reserve room for one array only (memory optimisation)? That’s what unions do (example 2 below).

Have you already evaluated std::variant?
http://en.cppreference.com/w/cpp/utility/variant

If you added details, maybe you got more elaborated answers.

Please note:
- if you decided to opt for standard library containers, the resulting code would be much simpler (example 3 below).
- as for your code, ‘starfighter’ dimensions are [16][15] rather then [17][17].

- - -

Example 1:
ToeMater.dat:
starfighter
B B B B B B B W B B B B B B B
B B B B B B B W B B B B B B B
B B B B B B W W W B B B B B B
B B B B B B W W W B B B B B B
B B B B B B W W W B B B B B B
B B B B R B W W W B R B B B B
B B B B R B W W W B R B B B B
B B B B W W W W W B W B B B B
R B B B Q B W R B B Q B B B R
R B B Q W B R R R W W Q B B R
W B B W W B R W R W W W B B W
W B W W W W W W W W W W W B W
W W W W W R W W W R W W W W W
W W W B R R W W W R R B W W W
W W B B R R B W B R R B B W W
W B B B B B B W B B B B B B W
*

moth
B Q B B B B Y B B B B Q B
B B Q B B R Y R B B Q B B
B B B Q R R Y R R Q B B B
B B B B Y Y Y Y Y B B B B
B B B Q Q Y Y Y Q Q B B B
B B Q Q Q R R R Q Q Q B B
B Q Q Q B R R R B Q Q Q B
Q Q Q Q B Y Y Y B Q Q Q Q
Q Q Q B B R R R B B Q Q Q
Q Q Q B B B R B B B Q Q Q
*

galaga
B B B B B B G B G B B B B B B
B B B B B B G B G B B B B B B
B B B G G O O G O O G G B B B
B B B B G O O G O O G B B B B
B B B B B G G G G G B B B B B
B B B B B G B G B B B B B B B
B B B B G Y Y G Y Y G B B B B
B B G G G Y Y Y Y Y G G G B B
G G G B B Y Y Y Y Y G G G G G
B G G B B Y Y Y Y Y G G G G B
B G O G G Y Y Y Y Y G G O G B
G G O G B B O B O B B G O G G
G O G G B B O B O B B G G O G
G O O G B B B B B B B G O O G
G O O G B B B B B B B G O O G
G G G G B B B B B B B G G G G
B G G B B B B B B B B B G G B
*

bullet_shape
B Q B
B Q B
Q Q Q
Q W Q
B R B
B R B
B R B
B R B
*


Example 1:
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
#include <chrono>
#include <fstream>
#include <iostream>
#include <random>
#include <string>
#include <tuple>
#include <vector>

using arr_info = std::tuple<std::string, unsigned, unsigned>;

const std::vector<arr_info> ARR_INFOS { std::make_tuple("starfighter", 16, 15),
                                        std::make_tuple("moth", 10, 13),
                                        std::make_tuple("galaga", 17, 15),
                                        std::make_tuple("bullet_shape", 8, 3) };

unsigned getRndUns(unsigned min, unsigned max);

int main()
{
    const arr_info ai = ARR_INFOS.at(getRndUns(0, ARR_INFOS.size() - 1));
    char* myarr = new char[std::get<1>(ai) * std::get<2>(ai)] {};
    std::ifstream in("ToeMater.dat");
    for(std::string label; std::getline(in, label); /**/) {
        if(label == std::get<0>(ai)) { break; }
    }
    std::cout << "Loading data for " << std::get<0>(ai) << '\n';
    unsigned i {}, j{};
    for(char c {}; in >> c && c != '*'; /**/) {
        if(c == '*') break;
        myarr[i * std::get<2>(ai) + j] = c;
        if(++j == std::get<2>(ai)) {
            j = 0;
            ++i;
        }
    }
    for(i = 0; i < std::get<1>(ai); ++i) {
        for(j = 0; j < std::get<2>(ai); ++j) {
            std::cout << myarr[i * std::get<2>(ai) + j] << ' ';
        }
        std::cout << '\n';
    }
    in.close();
    delete[] myarr;
}

unsigned getRndUns(unsigned min, unsigned max)
{
    static std::mt19937 eng { static_cast<unsigned>(
                               std::chrono::high_resolution_clock::now()
                                   .time_since_epoch()
                                   .count() ) };
    std::uniform_int_distribution<unsigned> ds(min, max);
    return ds(eng);
}


Example 2:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <iostream>

// Simplified example: these data should be read from a database!
const char mya[2][2] { { 'a', 'a' }, { 'a', 'a' } };
const char myb[2][3] { { 'b', 'b', 'b' }, { 'b', 'b', 'b' } };
const char myc[2][4] { { 'c', 'c', 'c', 'c' }, { 'c', 'c', 'c', 'c' } };

union U {
    char a[2][2];
    char b[2][3];
    char c[2][4] {};
};

int main()
{
    U myu;
    for(int i{}; i < 2; ++i) {
        for(int j{}; j < 3; ++j) { myu.b[i][j] = myb[i][j]; }
    }
    std::cout << "myb[1][2]: '" << myb[1][2]
              << "'; myu.b[1][2]: '" << myu.b[1][2] << "'\n";
}


Example 3:
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
#include <algorithm>
#include <chrono>
#include <fstream>
#include <iostream>
#include <random>
#include <string>
#include <vector>

const std::vector<std::string> ARR_NAMES { "starfighter",
                                           "moth",
                                           "galaga",
                                           "bullet_shape" };

unsigned getRndUns(unsigned min, unsigned max);

int main()
{
    const std::string thatone = ARR_NAMES.at(getRndUns(0, ARR_NAMES.size() - 1));

    std::ifstream in("ToeMater.dat");
    for(std::string label; std::getline(in, label); /**/) {
        if(label == thatone) { break; }
    }
    std::cout << "Loading data for " << thatone << '\n';
    std::vector<std::vector<char>> myv;
    for(std::string line; std::getline(in, line) && line.at(0) != '*'; /**/) {
        std::vector<char> tmp;
        std::copy_if(line.begin(), line.end(), std::back_inserter(tmp),
                        [](const char& a) { return a != ' '; }
                    );
        myv.push_back(tmp);
    }
    for(const auto& v : myv) {
        for(const char c : v ) { std::cout << c << ' '; }
        std::cout << '\n';
    }
    in.close();
}

unsigned getRndUns(unsigned min, unsigned max)
{
    static std::mt19937 eng { static_cast<unsigned>(
                               std::chrono::high_resolution_clock::now()
                                   .time_since_epoch()
                                   .count() ) };
    std::uniform_int_distribution<unsigned> ds(min, max);
    return ds(eng);
}

Topic archived. No new replies allowed.