The function is OK now, topic achieved. Thanks everyone.
I have the following very simple structure inside the class:
struct Data
{
string name1;
string name2;
string name3;
string name4;
}
I initialize:
Data **data;
data=new Data*[size];
The problem is to shift all the pointers to the elements of array from the position N to the right , so that I can insert a new element into N. I've tried various things, but everything ended up with an array of elements pointing to the same object...
I have the similar function which deletes the element and shifts everything to the left. It's working fine:
int Del_element(/*some parameters*/)
{
found=Bin_search(Name1,Name2); //binary search. Returns index of element.
if (found<0) return 0; //element wasn't found
delete data[found]; //delete pointer
for (int i=found; i<index-1; i++) //shift all pointers to the left
{
data[i]=data[i+1];
}
index--;
return 1;
}
The function which I implemented is below:
void re_size()
{
int size_old=size;
size*=2;
Data **tmp_array;
tmp_array = new Data*[size];
copy(data, data+size_old, tmp_array);
delete []data;
data=new Data*[size];
data=tmp_array;
}
int Add( const string & Name1,const string & Name2,const string & Name3, const string & Name4 )
{
int found=Bin_search(Name1,Name2); //binary search. Return positive number if found, or negative as the position to insert new element;
if (found>0) //if element already exists
{
return 0;
}
if ((index+1)==size) {re_size();}
data[index]= new Record(Name1,Name2,Name3,Name4);
if (index>0)
{
for (int i=index; i>-found; i--)
{
*data[i]=*data[i-1];
}
data[-found]->name1=Name1;
data[-found]->name2=Name1;
data[-found]->name3=Name1;
data[-found]->name4=Name1;
}
index++;
return 1;
}
Basically, I initialize new element at the end of array, then copy elements from the end of array to found position and copy values into data[-found].
You didn't show the non-working code, so I'll have to guess what you did wrong.
I'll bet you shifted element 0 into 1, 1 into 2, 2 into 3, etc. But when you shift 1 into 2, it contains what was originally in 0. And then when you shift 2 into 3, it contains what you just shifted from 1, which was originally in 0. And so on.
You need do the shifts starting from the end: N-1 into N, N-2 into N-1, N-3 into N-2, etc.
The fact that the things you're shifting are pointers is completely irrelevant, you'd have the same problem with an array of numbers.
First, Del_element
function is here to delete an element for new incoming pointer. If is it true, then it doesn't work because you just occupy freed element ( data[first]
) with new one ( data[first+1]
).
But if, you should be sure if you mean N
equals to size
and new incoming pointer must be places in size, it's OK.
However, you must show real and more complete code.
OK, that's better. First of all I think it is not so good to return false
in the function with int
return type. Second, I feel there is a big problem here for (int i=index-1; i>-found; i--)
. Why i>-found
?
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.