0

This following code:

enum Type {Prince, Princess, King, Queen, NumTypes};
enum Country {England, Belgium, Netherlands, NumCountries};

class Factory {
    static const std::array<std::array<int, NumTypes>, NumCountries> probabilities;
    static std::array<std::array<int, NumTypes>, NumCountries> initializeProbabilities() {
        std::array<std::array<int, NumTypes>, NumCountries> p;
        p[England] =     {29, 60, 80, 100};
        p[Belgium] =     {31, 66, 81, 100};
        p[Netherlands] = {25, 45, 90, 100};
      return p;
    }
};
const std::array<std::array<int, NumTypes>, NumCountries> Factory::probabilities = initializeProbabilities();

is safe if I ever change the order of elements in enum Country, but it is not safe from any future reordering of enum Type elements. What is the best way to avoid that problem without initializing all 12 elements one by one?

6
  • You could use a map... Commented Jul 27, 2014 at 21:51
  • @ Brian. But isn't that simply assigning all 12 elements one by one? Isn't there a way to do it without assigning each element individually? Imagine the matrix is 12 by 30! Commented Jul 27, 2014 at 21:52
  • If you have C++11 support (which you obviously do, given std::array), you can initialize maps with braced-init-lists. Commented Jul 27, 2014 at 21:57
  • But that would still force the first number to be Prince, the second Princess, etc..., so it faces the same problem as my code above. Commented Jul 27, 2014 at 21:59
  • No, it wouldn't. You initialize a map with a list of pairs. Commented Jul 27, 2014 at 21:59

2 Answers 2

1

In order to avoid dependency on the order, you should write something like:

p[England][Prince]=29;
p[England][Princess]=60;
p[England][King]=80;
p[England][Queen]=100;

p[Belgium][Prince]=31;
p[Belgium][Princess]=66;
p[Belgium][King]=81;
p[Belgium][Queen]=100;
Sign up to request clarification or add additional context in comments.

Comments

0

This is the solution suggested by Brian (I think this is what he meant). Is this probably the best way to solve the issues described?

enum Type {Prince, Princess, King, Queen, NumTypes};
enum Country {England, Belgium, Netherlands, NumCountries};

class Factory {
    static const std::array<std::map<Type, int>, NumCountries> probabilities;
    static std::array<std::map<Type, int>, NumCountries> initializeProbabilities() {
        std::array<std::map<Type, int>, NumCountries> p;
        p[England] =     { {Prince, 29}, {Princess, 60}, {King, 80}, {Queen, 100} };
        p[Belgium] =     { {Prince, 31}, {Princess, 66}, {King, 81}, {Queen, 100} };
        p[Netherlands] = { {Prince, 25}, {Princess, 45}, {King, 90}, {Queen, 100} };
        return p;   
    }
};
const std::array<std::map<Type, int>, NumCountries> Factory::probabilities = initializeProbabilities();

Or perhaps he meant map of a map.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.