C++Talk.NET Forum Index C++Talk.NET
C++ language newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

deleting instance in array
Goto page 1, 2  Next
 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ language (comp.lang.c++)
View previous topic :: View next topic  
Author Message
Dan
Guest





PostPosted: Fri Jun 25, 2004 8:39 pm    Post subject: deleting instance in array Reply with quote



hello,

I would to know if it is possible to delete an instance in an array, The
following does not allow me to do a delete.
I am trying to find and delete the duplicate in an array, thanks


for ( j =0; j {
for ( i =0; i {
if (array_3[i] == array_3[j + i] )
{delete array_3[i];
}

}
}



Dan



Back to top
Howard
Guest





PostPosted: Fri Jun 25, 2004 9:02 pm    Post subject: Re: deleting instance in array Reply with quote




"Dan" <Dan@ hotmail.com> wrote

Quote:
hello,

I would to know if it is possible to delete an instance in an array, The
following does not allow me to do a delete.
I am trying to find and delete the duplicate in an array, thanks


for ( j =0; j {
for ( i =0; i {
if (array_3[i] == array_3[j + i] )
{delete array_3[i];
}

}
}


What does not work? Does it crash?

How do you create each member of the array? If you use something like

array_3[i] = new someobjectyouhavenotshown;

then you're deleting it properly. This assumes that the array holds
pointers. Does it?

Or, are you expecting the ith item to go away, and all items beyond that to
shift up one space?

You'll need to be more specific about what the array contains, how it is
populated (i.e., how are its items created), and what you mean when you say
it doesn't allow you to do a delete.

Perhaps what you want to use is a std::vector, not an array. But without
more info, it's impossible to tell.

-Howard




Back to top
John Harrison
Guest





PostPosted: Fri Jun 25, 2004 9:53 pm    Post subject: Re: deleting instance in array Reply with quote




"Dan" <Dan@ hotmail.com> wrote

Quote:
hello,

I would to know if it is possible to delete an instance in an array, The
following does not allow me to do a delete.
I am trying to find and delete the duplicate in an array, thanks


for ( j =0; j {
for ( i =0; i {
if (array_3[i] == array_3[j + i] )
{delete array_3[i];
}

}
}


It is impossible to change the size of an array in C++, so you are trying to
do something rather difficult.

The easiest way to do this is to learn about vectors, which can be resized.
Get yourself a modern C++ book that teaches you about vectors.

john



Back to top
Victor Bazarov
Guest





PostPosted: Fri Jun 25, 2004 11:08 pm    Post subject: Re: deleting instance in array Reply with quote

Dan wrote:
Quote:
I would to know if it is possible to delete an instance in an array, The
following does not allow me to do a delete.
I am trying to find and delete the duplicate in an array, thanks

What would you expect the result to be? Say, I have an array
to begin with:

int int_array[] = { 1, 2, 3, 4, 3, 2 };

and I want to delete the duplicate '2'. What should the array
be after I finish "deleting" from it?

Quote:


for ( j =0; j {
for ( i =0; i {
if (array_3[i] == array_3[j + i] )
{delete array_3[i];
}

}
}

Back to top
Dan
Guest





PostPosted: Sat Jun 26, 2004 12:34 am    Post subject: Re: deleting instance in array Reply with quote


"Victor Bazarov" <v.Abazarov (AT) comAcast (DOT) net> wrote

Quote:
Dan wrote:
I would to know if it is possible to delete an instance in an array, The
following does not allow me to do a delete.
I am trying to find and delete the duplicate in an array, thanks

What would you expect the result to be? Say, I have an array
to begin with:

int int_array[] = { 1, 2, 3, 4, 3, 2 };
and I want to delete the duplicate '2'. What should the array
be after I finish "deleting" from it?



Yes the idea was to delete only the duplicate entry,
so the above would be 1,2,3,4.
But that has a problem because the size of the array would change.
So then the second step would be creating a dynamic array.
But now I cannot seem to delete that duplicate value.






Quote:
for ( j =0; j {
for ( i =0; i {
if (array_3[i] == array_3[j + i] )
{delete array_3[i];
}

}
}



Back to top
John Harrison
Guest





PostPosted: Sat Jun 26, 2004 5:56 am    Post subject: Re: deleting instance in array Reply with quote


"Dan" <Dan@ hotmail.com> wrote

Quote:

"Victor Bazarov" <v.Abazarov (AT) comAcast (DOT) net> wrote in message
news:2g2Dc.13$Wd.779 (AT) ord-read (DOT) news.verio.net...
Dan wrote:
I would to know if it is possible to delete an instance in an array,
The
following does not allow me to do a delete.
I am trying to find and delete the duplicate in an array, thanks

What would you expect the result to be? Say, I have an array
to begin with:

int int_array[] = { 1, 2, 3, 4, 3, 2 };
and I want to delete the duplicate '2'. What should the array
be after I finish "deleting" from it?



Yes the idea was to delete only the duplicate entry,
so the above would be 1,2,3,4.
But that has a problem because the size of the array would change.
So then the second step would be creating a dynamic array.
But now I cannot seem to delete that duplicate value.


I would stop uses the term delete, as I'm sure you know delete refer to
something quite different in C++ from what you are trying to do. Lets just
say you are trying to remove duplicate entries. And the only way to do that
is to overwrite entries with other ones. For instance

1, 2, 3, 2, 4, 5

goes to

1, 2, 3, 4, 5, 5

The second 2 has been removed by copying all the higher elements (4 and 5)
down one position. Now of course we have an extra 5 at the end, but that
doesn't matter because we are going to shorten the array by one, so that
extra 5 will be removed when that happens.

Some questions

1) Are you prepared to sort the array, or must the elements stay in the
order they are already? The algorithm you should use is simpler and more
efficient if you are prepared to sort the array (because in a sorted array
the duplicate elements are adjacent).

2) Are you prepared to use a vector? It could literally be one line of code
if you are. In a perfect world there would be no reason at all not to use a
vector for this problem, but I realise that maybe you have never used one
before and you want to stick with what you know. But in truth vectors are
easier than dynamic arrays, vectors are what newbies should use all the time
while dynamic arrays are what experts should use sometimes (most of the time
experts should use vectors as well).

john



Back to top
Gianni Mariani
Guest





PostPosted: Sat Jun 26, 2004 7:56 am    Post subject: Re: deleting instance in array Reply with quote

Dan wrote:
Quote:
hello,

I would to know if it is possible to delete an instance in an array, The
following does not allow me to do a delete.
I am trying to find and delete the duplicate in an array, thanks


for ( j =0; j<MAX ; j++)
{
for ( i =0; i {
if (array_3[i] == array_3[j + i] )
{delete array_3[i];
}

}
}


You can call the destructor on an array element, but you'd be advised to
construct it again -- see below.


#include
template <typename Tz>
Tz * Create( void * i_location )
{
return new ( i_location ) Tz();
}


template <typename T>
void Destroy( T & i_val )
{
i_val.~T();

void * i_addr = static_cast< void * >( & i_val );

Create<T>( i_addr );
}



struct A
{

A()
{
}

~A()
{
}

int z;
};


A a[20];
int i[20];

int main()
{

Destroy( a[3] );

Destroy( i[3] );
}


Back to top
Dan
Guest





PostPosted: Sat Jun 26, 2004 9:42 am    Post subject: Re: deleting instance in array Reply with quote


Quote:
Yes the idea was to delete only the duplicate entry,
so the above would be 1,2,3,4.
But that has a problem because the size of the array would change.
So then the second step would be creating a dynamic array.
But now I cannot seem to delete that duplicate value.


I would stop uses the term delete, as I'm sure you know delete refer to
something quite different in C++ from what you are trying to do. Lets just
say you are trying to remove duplicate entries. And the only way to do
that
is to overwrite entries with other ones. For instance

1, 2, 3, 2, 4, 5

goes to

1, 2, 3, 4, 5, 5

The second 2 has been removed by copying all the higher elements (4 and 5)
down one position. Now of course we have an extra 5 at the end, but that
doesn't matter because we are going to shorten the array by one, so that
extra 5 will be removed when that happens.

Some questions

1) Are you prepared to sort the array, or must the elements stay in the
order they are already? The algorithm you should use is simpler and more
efficient if you are prepared to sort the array (because in a sorted array
the duplicate elements are adjacent).

I originally had two on sorted arrays, I combined into a third array, then I
sorted them . Now I have to eliminate the duplicates.
But juste one question when the duplicates are removed, we just cout a
shorter array, we dont really shorten the array right ?


Quote:

2) Are you prepared to use a vector? It could literally be one line of
code
if you are. In a perfect world there would be no reason at all not to use
a
vector for this problem, but I realise that maybe you have never used one
before and you want to stick with what you know. But in truth vectors are
easier than dynamic arrays, vectors are what newbies should use all the
time
while dynamic arrays are what experts should use sometimes (most of the
time
experts should use vectors as well).
It is something to look into and I will , but my class does not ask for

this at the moment



Quote:

john





Back to top
John Harrison
Guest





PostPosted: Sat Jun 26, 2004 10:56 am    Post subject: Re: deleting instance in array Reply with quote


"Dan" <Dan@ hotmail.com> wrote

Quote:

Yes the idea was to delete only the duplicate entry,
so the above would be 1,2,3,4.
But that has a problem because the size of the array would change.
So then the second step would be creating a dynamic array.
But now I cannot seem to delete that duplicate value.


I would stop uses the term delete, as I'm sure you know delete refer to
something quite different in C++ from what you are trying to do. Lets
just
say you are trying to remove duplicate entries. And the only way to do
that
is to overwrite entries with other ones. For instance

1, 2, 3, 2, 4, 5

goes to

1, 2, 3, 4, 5, 5

The second 2 has been removed by copying all the higher elements (4 and
5)
down one position. Now of course we have an extra 5 at the end, but that
doesn't matter because we are going to shorten the array by one, so that
extra 5 will be removed when that happens.

Some questions

1) Are you prepared to sort the array, or must the elements stay in the
order they are already? The algorithm you should use is simpler and more
efficient if you are prepared to sort the array (because in a sorted
array
the duplicate elements are adjacent).

I originally had two on sorted arrays, I combined into a third array, then
I
sorted them . Now I have to eliminate the duplicates.
But juste one question when the duplicates are removed, we just cout a
shorter array, we dont really shorten the array right ?

Yes that's fine, but it wasn't clear from your original post whether that
was a possibility.

Here's some sample code, it only works on an already sorted array.

int remove_dups(int* array, int old_size)
{
int new_size = 1;
for (int i = 1; i < old_size; ++i)
if (array[i] != array[i - 1])
array[new_size++] = array[i];
return new_size;
}

int main()
{
int a[7] = { 1, 1, 1, 2, 3, 5, 5 };
int size = sizeof a/sizeof a[0];
size = remove_dups(a, size);
for (int i = 0; i < size; ++i)
cout << a[i] << ' ';
cout << 'n';
}

As you can see remove_dups returns the new size of the array, after the
duplicates have been removed.

john



Back to top
Dan
Guest





PostPosted: Sun Jun 27, 2004 9:24 pm    Post subject: Re: deleting instance in array Reply with quote

Quote:
Yes that's fine, but it wasn't clear from your original post whether that
was a possibility.

Here's some sample code, it only works on an already sorted array.

int remove_dups(int* array, int old_size)
{
int new_size = 1;
for (int i = 1; i < old_size; ++i)
if (array[i] != array[i - 1])
array[new_size++] = array[i];
return new_size;
}

int main()
{
int a[7] = { 1, 1, 1, 2, 3, 5, 5 };
int size = sizeof a/sizeof a[0];
size = remove_dups(a, size);
for (int i = 0; i < size; ++i)
cout << a[i] << ' ';
cout << 'n';
}

As you can see remove_dups returns the new size of the array, after the
duplicates have been removed.

john

WOw thats great coding, nice , easy and short
thanks a lot, I am going to study this for a while now.



Back to top
John Harrison
Guest





PostPosted: Mon Jun 28, 2004 6:02 am    Post subject: Re: deleting instance in array Reply with quote

Quote:

int remove_dups(int* array, int old_size)
{
int new_size = 1;
for (int i = 1; i < old_size; ++i)
if (array[i] != array[i - 1])
array[new_size++] = array[i];
return new_size;
}


WOw thats great coding, nice , easy and short
thanks a lot, I am going to study this for a while now.


Thanks, but actually its bugged. It will fail if the old_size is zero, in
that case new_size will equal 1 which is obviously incorrect. Replace with
this slight modification

int remove_dups(int* array, int old_size)
{
if (old_size == 0)
return 0;
int new_size = 1;
for (int i = 1; i < old_size; ++i)
if (array[i] != array[i - 1])
array[new_size++] = array[i];
return new_size;
}

john



Back to top
Julie
Guest





PostPosted: Mon Jun 28, 2004 4:20 pm    Post subject: Re: deleting instance in array Reply with quote

John Harrison wrote:
Quote:

"Dan" <Dan@ hotmail.com> wrote in message
news:c40Dc.172$LV5.1099 (AT) news20 (DOT) bellglobal.com...
hello,

I would to know if it is possible to delete an instance in an array, The
following does not allow me to do a delete.
I am trying to find and delete the duplicate in an array, thanks

....

Quote:
It is impossible to change the size of an array in C++, so you are trying to
do something rather difficult.

???

What is happening here then?

size_t size = 2;
int * array = (int *)malloc(sizeof(int) * size);
array[0] = 42;
array[1] = 9000;
array = (int *)realloc(array, sizeof(int) * (--size));

As near as I can tell, at the start, there is an array of size 2, at the end,
it has a size of 1. Please explain what I'm missing or why it isn't possible.

Back to top
John Harrison
Guest





PostPosted: Mon Jun 28, 2004 4:34 pm    Post subject: Re: deleting instance in array Reply with quote


"Julie" <julie (AT) nospam (DOT) com> wrote

Quote:
John Harrison wrote:

"Dan" <Dan@ hotmail.com> wrote in message
news:c40Dc.172$LV5.1099 (AT) news20 (DOT) bellglobal.com...
hello,

I would to know if it is possible to delete an instance in an array,
The
following does not allow me to do a delete.
I am trying to find and delete the duplicate in an array, thanks

...

It is impossible to change the size of an array in C++, so you are
trying to
do something rather difficult.

???

What is happening here then?

size_t size = 2;
int * array = (int *)malloc(sizeof(int) * size);
array[0] = 42;
array[1] = 9000;
array = (int *)realloc(array, sizeof(int) * (--size));

As near as I can tell, at the start, there is an array of size 2, at the
end,
it has a size of 1. Please explain what I'm missing or why it isn't
possible.


That's a pointer to allocated memory not an array. It was unclear from the
OP post whether he was dealing with pointers or arrays, I said 'that's
impossible' in order to try and provoke a reaction and get him to explain
himself a little more.

john



Back to top
Victor Bazarov
Guest





PostPosted: Mon Jun 28, 2004 5:15 pm    Post subject: Re: deleting instance in array Reply with quote

Julie wrote:
Quote:
John Harrison wrote:

"Dan" <Dan@ hotmail.com> wrote in message
news:c40Dc.172$LV5.1099 (AT) news20 (DOT) bellglobal.com...

hello,

I would to know if it is possible to delete an instance in an array, The
following does not allow me to do a delete.
I am trying to find and delete the duplicate in an array, thanks


...


It is impossible to change the size of an array in C++, so you are trying to
do something rather difficult.


???

What is happening here then?

size_t size = 2;
int * array = (int *)malloc(sizeof(int) * size);
array[0] = 42;
array[1] = 9000;
array = (int *)realloc(array, sizeof(int) * (--size));

As near as I can tell, at the start, there is an array of size 2, at the end,
it has a size of 1. Please explain what I'm missing or why it isn't possible.

By calling 'realloc' you're not resizing an array, you're getting
another array of a potentially different size with some elements that
have the same values as the source array.

I don't have a copy of C99 (or C90) Standard handy, but AFAICT, the
values of the address you pass to 'realloc' (as the first argument)
and the address you get back from 'realloc' are never the same (unless
it fails to allocate and returns NULL and you passed NULL to begin
with).

V

Back to top
Julie
Guest





PostPosted: Mon Jun 28, 2004 5:53 pm    Post subject: Re: deleting instance in array Reply with quote

John Harrison wrote:
Quote:

"Julie" <julie (AT) nospam (DOT) com> wrote in message
news:40E04530.DBD89697 (AT) nospam (DOT) com...
John Harrison wrote:

"Dan" <Dan@ hotmail.com> wrote in message
news:c40Dc.172$LV5.1099 (AT) news20 (DOT) bellglobal.com...
hello,

I would to know if it is possible to delete an instance in an array,
The
following does not allow me to do a delete.
I am trying to find and delete the duplicate in an array, thanks

...

It is impossible to change the size of an array in C++, so you are
trying to
do something rather difficult.

???

What is happening here then?

size_t size = 2;
int * array = (int *)malloc(sizeof(int) * size);
array[0] = 42;
array[1] = 9000;
array = (int *)realloc(array, sizeof(int) * (--size));

As near as I can tell, at the start, there is an array of size 2, at the
end,
it has a size of 1. Please explain what I'm missing or why it isn't
possible.

That's a pointer to allocated memory not an array. It was unclear from the
OP post whether he was dealing with pointers or arrays, I said 'that's
impossible' in order to try and provoke a reaction and get him to explain
himself a little more.

john

Ok, we are getting into a pretty gray area here by making that distinction.
Yes, I do agree that there is a distinction conceptually, but implementation,
it is the same, right?

Back to top
Display posts from previous:   
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ language (comp.lang.c++) All times are GMT
Goto page 1, 2  Next
Page 1 of 2

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.