dynamic array
May 28, 2009 at 6:16am UTC
I need some help for dynamic array.
I want to delete an element from massiv pointer.
i wrote in devc++.
thnks for ur help
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
int *xe, *ye;
xe = new int [OBJ_NUM];
ye = new int [OBJ_NUM];
for (int j = 0; j < OBJ_NUM; j++)
{
if (xe[j] <= 1 || ye[j] <= 1 || xe[j] >= 10 || ye[j] >= 10)
{
xe[j] = 0;
ye[j] = 0;
if (xe[j] == 0 || ye[j] == 0)
{
delete xe[j]; //error
delete ye[j]; //error
OBJ_NUM--;
}
}
}
For example
int st[10]
and I want to remove the element at st[8], give it a total of 9 elements, now.
This's, I only want only 9 elements in my array now!
Last edited on May 28, 2009 at 6:30am UTC
May 28, 2009 at 6:40am UTC
Do you understand what you want?
If it's, please see my variant
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
#include <vector>
int *xe, *ye;
xe = new int [OBJ_NUM];
ye = new int [OBJ_NUM];
std::vector<int > xeContainer;
std::vector<int > yeContainer;
for (int j = 0; j < OBJ_NUM; j++)
{
if (!(xe[j] <= 1 || ye[j] <= 1 || xe[j] >= 10 || ye[j] >= 10))
{
xeContainer.push_back(xe[j])
yeContainer.push_back(ye[j]);
}
else
{
OBJ_NUM--;
}
}
delete []xe;
delete []ye;
May 28, 2009 at 6:41am UTC
According to your question about deleting array element in heap.
It's impossible. If you want to do it in any case you have to create new array and delete old one.
May 28, 2009 at 7:27am UTC
i just need to delete n found number of elements without using vector, but using pointer.
1 2 3 4 5 6 7 8 9 10 11 12
for (int j = 0; j < OBJ_NUM; j++)
{
for (int i = 0; i < SCAN_NUMBER ; i++)
{
if (obs_x[j][i] <= 1 || obs_y[j][i] <= 1 || obs_x[j][i] >= l_x-1 || obs_y[j][i] >= l_y-1)
{
delete [] obs_x[i]; //it's working wrong
delete [] obs_y[i]; //delete j-th obs if found in if
OBJ_NUM--; //end decrease number of obs
}
}
}
Last edited on May 28, 2009 at 7:32am UTC
May 28, 2009 at 9:53am UTC
According to your question about deleting array element in heap.
But you can create a new array. Then delete old one
May 28, 2009 at 5:35pm UTC
For example
int st[10]
and I want to remove the element at st[8], give it a total of 9 elements, now.
This's, I only want only 9 elements in my array now!
You can't chop peices out of an array like that.
You would have to create a new array of the new size and copy across the elements you need from the old array.
Then delete the old array (as suggested by
Denis )
Topic archived. No new replies allowed.