简体   繁体   中英

c++ Declaring a 2D array as a global variable

I am struggling to figure out how to declare a 2D Array as a global variable so i can use it in all of my methods. So far it is only declared in a single method hence why it cannot be used in other methods.I have figured out how to declare a normal string array by just typing string* array = new string[1] at the start of my code before the methods (i then alter the size of this array later on based of a variable) but i am unsure how to do it with a 2D array:

void WordSearch::ReadSimplePuzzle()

int columns = 9;

int rows = 9;


string **simple2DArray = new string*[columns];

for (int i = 0; i < columns; i++)

         simple2DArray[i] = new string[rows];

//code that populates the array too long to post but not relevant.

I then have a method later on where i need to access the simple2DArray but i cannot figure out how to define it at the start of the code any help would be appreciated.

If you columns and rows variables never change, you can do this:

const int columns = 9;
const int rows = 9;

string simple2DArray[columns][rows];

By statically allocating the memory, you now don't have to worry about freeing it.


Since you clarified that the size is not known until run-time, you will not be able to allocate the memory statically. A very simple solution would be:

std::vector<std::vector<std::string>> simple2DArray; // This will have size 0 at start

Then, in your initialization step, just do this:

simple2DArray.resize(rows);
for (auto& row : simple2DArray)
{
    row.resize(columns);
}

There are other ways to do this, of course, such as allocating all the memory in one block of size rows*columns and then exposing it as if it were a 2-d matrix but that might be overkill for your purposes.

My suggestion is hide the array behind a functional interface.

std::string const& getElement(size_t m, size_t n);
void setElement(size_t m, size_t n, std::string const& val);

The calling functions have the abstractions of a 2D array but they don't need to know how the it is represented in code.

In the implementation, you have various options:

  1. Use a 1D array. Map the 2D indices to the right index in the 1D array.
  2. Use a std::vector . Still need to map the indices.
  3. Use a 2D array. No mapping of indices needed.
  4. Use a std::vector<std::vector<std::string>> . No mapping of indices needed.

I am struggling to figure out how to declare a 2D Array as a global variable so i can use it in all of my methods.

As with any global var, you need to declare your pointer in global space:

string **simple2DArray;

and then you can assign to it from inside your method

simple2DArray = new string*[columns];

If you are asking this for making it easier to solve competitive programming problems, then look at the constraints given in the question.

For example if the matrix can be an N*N with 1 <= N <= 1000 Then you can globally declare
int arr[1000][1000];
Here's some code for a better idea.

//global declarations 
int N;
int arr[1000][1000];

int functionA()
{
  // some code
}

int functionB()
{
   // some code
} 

int main()
{
   // Get the input of both N and your array arr
   // Now you can use them in any where in your code 
    
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM