Dynamic array
-
Hi! Do somebody know, how to create an dynamical array? I tried to do something like that: int count = 0; while (s == newName) { int i = atoi(s); count ++; int arr[count]; cin >> arr[i]; } As long, as it founds some elements in a list, it should size the length of the array and fill it. Thanks -- modified at 2:41 Saturday 1st April, 2006
-
Hi! Do somebody know, how to create an dynamical array? I tried to do something like that: int count = 0; while (s == newName) { int i = atoi(s); count ++; int arr[count]; cin >> arr[i]; } As long, as it founds some elements in a list, it should size the length of the array and fill it. Thanks -- modified at 2:41 Saturday 1st April, 2006
Replace these lines:
int arr[count]; cin >> arr[i];
With this:int* pArray = new int[count]; cin >> pArray[i]
Then after you are done using the array:delete [] pArray;
John -- modified at 3:01 Saturday 1st April, 2006 -
Hi! Do somebody know, how to create an dynamical array? I tried to do something like that: int count = 0; while (s == newName) { int i = atoi(s); count ++; int arr[count]; cin >> arr[i]; } As long, as it founds some elements in a list, it should size the length of the array and fill it. Thanks -- modified at 2:41 Saturday 1st April, 2006
First include this:
#include <vector>
Now we'll make a typedef for a vector (essentially dynamic array) ofint
s.typedef std::vector<int> IntArray;
Now he's your code rewritten to use the vector:int count = 0; IntArray arr; while (s == newName) { int i = atoi(s); coun++; int num; cin >> num; arr.push_back(num); }
This code look like it would get in an infinite loop to me - But that's another story. Steve