-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.

4
  • 5
    This is C++, use vectors std::vector<std::vector<std::string>> A(...). Commented Jun 29, 2018 at 8:56
  • 2
    In c++ you should use std::vector or std::array instead of those new any_type[m][n] Commented Jun 29, 2018 at 8:56
  • 1
    Possible duplicate of Initializing a two dimensional array of strings, How can i declare and init two dimensional array of strings, etc. Commented Jun 29, 2018 at 9:00
  • 2
    VLAs (variable length array don't exist in C++) Commented Jun 29, 2018 at 9:12

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

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.