multi dimentional dynamic arrays....

HAI!

I know how to use multi dimentional arrays and dynamic arrays..
but now i need both of them together..

string* foo = new string[2][2];

for instance gives off a bunch of errors...
I tried several different things but i can't get it to work. and yes i've googled..
Also I don't want to use a vector because i hear they are very memory consuming (need good memory effieciency)

KTHXBYE!
Last edited on
This is one way.
1
2
3
4
5
string **foo = new string*[2]; //allocate an array of string pointers
for( int i = 0; i < 2; i++ )
 foo[i] = new string[2]; //allocate an array of strings for each pointer
 //foo at i is a string* because foo is a string**
//remember to deallocate all of these at the end! 
Last edited on
This is another.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
class Matrix
{
  int columns;
  string *foo;

 public:
  Matrix(int rows, int cols) {
    columns = cols;
    foo = new string[rows * cols];
  }
  ~Matrix() { delete foo; }

  string get(int row, int col) {
    return foo[row * columns + col];
  }

  void set(int row, int col, string val) {
    foo[row * column + col] = val;
  }
}
Topic archived. No new replies allowed.