-1

I am trying to initialized 2D array of string in c++.

std::string A = new std::string [m+1][n+1]

But this is giving me error as array size in new-expression must be constant.

  • 5
    This is C++, use vectors `std::vector> A(...)`. – Qubit Jun 29 '18 at 08:56
  • 2
    In c++ you should use `std::vector` or `std::array` instead of those `new any_type[m][n]` – t.niese Jun 29 '18 at 08:56
  • 1
    Possible duplicate of [Initializing a two dimensional array of strings](https://stackoverflow.com/q/4612328/608639), [How can i declare and init two dimensional array of strings](https://stackoverflow.com/q/16836595/608639), etc. – jww Jun 29 '18 at 09:00
  • 2
    VLAs (variable length array don't exist in C++) – Jabberwocky Jun 29 '18 at 09:12

1 Answers1

0

You could do it the C-Style String way...

char ** array = new char*[10];
for (int i = 0; i < 10; ++i) 
{
     array[i] = new char[100];
}

Then you just delete it in the opposite order you created it when done.

Using the STL is probably a good choice if you need to be safe and modern.

Bayleef
  • 185
  • 2
  • 10