Hello
I need a 2-dimensional dynamic bool array with 2 fixed columns and dynamic number of rows.
What would be the proper way of implementing it?Code:bool *name[2]; name = new bool..?
Thanks a lot for help!
Hello
I need a 2-dimensional dynamic bool array with 2 fixed columns and dynamic number of rows.
What would be the proper way of implementing it?Code:bool *name[2]; name = new bool..?
Thanks a lot for help!
Apart from all the weird specialization stuff, what's wrong with vector< vector<bool> >?
Or vector<bool> name_col1, vector<bool> name_col2. Or vector<bool> name[2]?
Because I'm not allowed to use vector.
Do you ever have to resize? Or can you just say new bool[some_number][2] and be done with it?
Can you use std::pair? Then you could use a normal dynamic array of pairs.
I didn't realize at first you wanted fixed size columns instead of rows. I don't know the proper syntax off the top of my head. My guess is that it involves strategically placed parentheses.
Edit: tabstop's idea is probably best.
Isn't it just this?
(m and n are arbitrary)Code:name[0] = new bool[ m ]; name[1] = new bool[ n ];
What is wrong with using bool *name = new bool[ width * height]?
I dont have to resize it once its allocated.
Oh ok. Well how about this then:
Example:
I dunno if anyone had suggested that before, but it sounds good to me.Code:bool *name = new bool[width * height];
This will give me an error:
Code:bool **arr; arr= new bool[x][2];
and then dynamically allocate the rows (name[][0] and name[][1]).Code:bool** name = new bool*[2];
Last edited by abachler; 11-04-2008 at 04:58 PM.
Yep Behold!
Example:
Code:#include <iostream> #define WIDTH 5 #define WIDTH 5 inline bool &getValue(bool *name, size_t x, size_t y) { return name[y*WIDTH + x]; } int main(void) { bool *name = new bool[WIDTH * HEIGHT]; // do stuff.. std::cout << getValue(name, 3, 4) << std::endl; return 0; }