1

Is there a way to initialize two dimensional array with different size of columns in c++? I try to make console card game. I have done something like this while ago.

 static string const group[7][];

 string const Cards::group = {
            { "AA", "KK", "AKs" },
            { "QQ", "JJ", "AK", "AJs", "KQs", "AQs" },
            { "TT", "AQ", "ATs", "KJs", "QJs", "JTs" },
            { "99", "88", "AJ", "AT", "KQ", "KTs", "QTs", "J9s" , "T9s" , "98s" },
            { "77", "66", "A9s", "A5s", "A4s", "A3s", "A2s", "K9s", "KJ", "KT", "QJ", "QT", "Q9s", "JT", "QJ", "T8s", "97s", "87s", "76s", "65s"},
            { "55", "44", "33", "22", "K9", "J9", "86s"},
            { "T9", "98", "85s"}
    };

It doesn't work. It would be great if an array would be additionally static and constant but it isn't necessary. The most important thing is to make code works.

I would be grateful for any kind of help.

dagi12
  • 449
  • 1
  • 5
  • 20

3 Answers3

2

Arrays in C++ are not truly dynamic; they cannot be extended nor reduced in size. Moreover, the dimensions of static arrays have to be known at their declaration. Dynamically-allocated arrays can be used by declaring group as a string** and later initializing it with the appropriately-sized memory. You can also use a 2-dimensional vector which is fundamentally equivalent but preferred because it is cleaner because we can leverage its memory management:

static std::vector<std::vector<std::string>> const group;

std::vector<std::vector<std::string>> Cards::group = {
    { "AA", "KK", "AKs" },
    { "QQ", "JJ", "AK", "AJs", "KQs", "AQs" },
    { "TT", "AQ", "ATs", "KJs", "QJs", "JTs" },
    { "99", "88", "AJ", "AT", "KQ", "KTs", "QTs", "J9s" , "T9s" , "98s" },
    { "77", "66", "A9s", "A5s", "A4s", "A3s", "A2s", "K9s", "KJ", "KT", "QJ", "QT", "Q9s", "JT", "QJ", "T8s", "97s", "87s", "76s", "65s"},
    { "55", "44", "33", "22", "K9", "J9", "86s"},
    { "T9", "98", "85s"}
};
David G
  • 94,763
  • 41
  • 167
  • 253
  • You need C++11 in order to initialize a vector this way (using a list of values in braces), don't you? So it would depend on which compiler you have. Some of us are still using VC++2010 at this time. – David K Jul 12 '14 at 01:32
2

You can't use:

string group[7][] = 

However, you can use:

std::vector<std::string> group[] = 

Here's a working program.

#include <iostream>
#include <string>
#include <vector>

std::vector<std::string> group[] = 
{
   { "AA", "KK", "AKs" },
   { "QQ", "JJ", "AK", "AJs", "KQs", "AQs" },
   { "TT", "AQ", "ATs", "KJs", "QJs", "JTs" },
   { "99", "88", "AJ", "AT", "KQ", "KTs", "QTs", "J9s" , "T9s" , "98s" },
   { "77", "66", "A9s", "A5s", "A4s", "A3s", "A2s", "K9s", "KJ", "KT", "QJ", "QT", "Q9s", "JT", "QJ", "T8s", "97s", "87s", "76s", "65s"},
   { "55", "44", "33", "22", "K9", "J9", "86s"},
   { "T9", "98", "85s"}
};

int main()
{
   for ( auto it1 : group )
   {
      for ( auto it2 : it1 )
      {
         std::cout << it2 << " ";
      }
      std::cout << std::endl;
   }
}
R Sahu
  • 204,454
  • 14
  • 159
  • 270
1

You cannot do

string group[7][];

Instead, you can only do

 string group[][7];

for multidimensional C-style arrays.

If you truly need multidimensional array with different sizes in the last dimension, you can use std::vector> with STL.

thor
  • 21,418
  • 31
  • 87
  • 173