is this possible? if so, why doesnt the statement
work? any help would be *greatly* appreciated. thanks!Code:char *data=new char[w][h];
is this possible? if so, why doesnt the statement
work? any help would be *greatly* appreciated. thanks!Code:char *data=new char[w][h];
It's possible. You need to allocate space for the width first, then for each row (0 to w-1) allocate space for the length;
any hints as to how i could go about doing this?
>any hints as to how i could go about doing this?
Code:char **array = new char*[w]; for ( int i = 0; i < w; i++ ) array[i] = new char[h];
My best code is written with the delete key.
perfect! thanks a bunch!
Awww, that's not a hint, that's an answer!! A hint would be:Originally posted by Prelude
>any hints as to how i could go about doing this?
Code:char **array = new char*[w]; for ( int i = 0; i < w; i++ ) array[i] = new char[h];Originally posted by jlou
You need to allocate space for the width first, then for each row (0 to w-1) allocate space for the length;
cool, i was just going to post a question about this very same thing but it's already here. my question, though, is how would go about deleting the allocated 2d array?
is this the correct way to delete that dynamic 2d array?:
Code:for(int i = 0; i < w; i++) { delete [] array[i]; } delete [] array;
how about using STL vector to create a 2d dynamic array ?
how to create it and how to assign data or delete data from it ?
>>how to create it and how to assign data or delete data from it ?
http://cppreference.com/cppvector.html
some entropy with that sink? entropysink.com
there are two cardinal sins from which all others spring: Impatience and Laziness. - franz kafka
i mean creating a 2d dynamic array using STL vector. can i do something like
if the above code make a 2d dynamic array, how i insert data into it ?Code:vector<vector<int> >array;
Yes you can do that. Here's an example that show how to add stuff to a 2d vector:Code:#include <vector> int main() { int rows = 5; int cols = 10; // Start out full of 0's. std::vector<std::vector<int> > array0(rows, std::vector<int>(cols)); // Start out full of 1's. std::vector<std::vector<int> > array1(rows, std::vector<int>(cols, 1)); // Start out with empty rows. std::vector<std::vector<int> > array2(rows); for (int i = 0; i < rows; i++) { for (int j = 0; j < cols; j++) array2[i].push_back(j); } // Start out completely empty. std::vector<std::vector<int> > array; for (int i = 0; i < rows; i++) { std::vector<int> row; for (int j = 0; j < cols; j++) row.push_back(j); array.push_back(row); } }
Last edited by jlou; 03-09-2004 at 02:19 PM.
if i want to insert data in a specific location, then how ?
for example, if i need to insert data in location array[2][3], how am i suppose to do it ?
the vector class allows you to use the [] operator just as you would with standard arrays.
Using the insert methods of the vector class spares you the need to shift elements within the vector, if needed, but requires you to pass several parameters, one of which is an iterator to the address where you want to insert the material.
To, go one step further, there are matrix classes built on the vector classes that you could consider using. There, the underlying vector < vector < T> > stuff is all done for you. A search of the internet will probably dredge up several examples, eventually.
Just like in standard arrays, you can use:
array[2][3] = 78;
But remember that just like in a standard array you are not inserting data. Instead you are changing the data at that location. In both vectors and standard arrays, if you use [] there is already a value at the position you are accessing. The reason this is important is that if you start out with a completely empty array (e.g. std::vector<std::vector<int> > array;), then assigning a value to [2][3] is undefined behavior that could easily crash your program. You have to make sure the size of each vector is what you want it to be before using the operator[].
All four of those examples above leave the arrays in a state where you can use operator[].