473,748 Members | 9,596 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

deleting instance in array

Dan
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<MAX ; i++)
{
if (array_3[i] == array_3[j + i] )
{delete array_3[i];
}

}
}

Dan

Jul 22 '05 #1
18 2480

"Dan" <Dan@ hotmail.com> wrote in message
news:c4******** ********@news20 .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
for ( j =0; j<MAX ; j++)
{
for ( i =0; i<MAX ; 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 someobjectyouha venotshown;

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

Jul 22 '05 #2

"Dan" <Dan@ hotmail.com> wrote in message
news:c4******** ********@news20 .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
for ( j =0; j<MAX ; j++)
{
for ( i =0; i<MAX ; 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
Jul 22 '05 #3
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?


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

}
}

Jul 22 '05 #4
Dan

"Victor Bazarov" <v.********@com Acast.net> wrote in message
news:2g******** *****@ord-read.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.

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

}
}

Jul 22 '05 #5

"Dan" <Dan@ hotmail.com> wrote in message
news:Iw******** *******@news20. bellglobal.com. ..

"Victor Bazarov" <v.********@com Acast.net> wrote in message
news:2g******** *****@ord-read.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
Jul 22 '05 #6
Dan wrote:
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<MAX ; 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 <new>

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] );
}

Jul 22 '05 #7
Dan
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 ?


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

john

Jul 22 '05 #8

"Dan" <Dan@ hotmail.com> wrote in message
news:Fx******** ********@news20 .bellglobal.com ...
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
Jul 22 '05 #9
Dan
> 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.
Jul 22 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
11725
by: ms_chika | last post by:
Hi again to all, I just want to know how to delete array or array object in javascript? I have this problem, i have an html table that contains textboxes and delete button for each row, the contents of textboxes for each row are stored in an array. When i click the delete button it should delete the row from the table and also delete the row from the array that contains the data, but the problem is it is not deleted from the array...
4
14026
by: al havrilla | last post by:
hi all what does the phrase: "scalar deleting destructor" mean? i'm getting this in a debug error message using c++ 7.1 thanks Al
0
1408
by: Bandit | last post by:
I'm populating a gridview (called docList) with document info from a network folder like so: Sub Show_Files(ByVal inputDir As String) Dim strFileNamePath As String = inputDir Dim dirInfo As DirectoryInfo = New DirectoryInfo(strFileNamePath) Dim aFiles As FileInfo() = dirInfo.GetFiles("*.doc") Array.Sort(aFiles, New CompareFileInfoEntries (CompareByOptions.LastWriteTime)) docList.DataSource = aFiles
4
1654
by: Jim Michaels | last post by:
Is it safe to do what is below? deleting from a resultset while you are processing it? I don't know how dynamic the SQL database is. I assume you get a cursor to live dataset. Even if it is a cursor as opposed to a table, will the cursor's integrity be ruined if the current record is deleted? $q2=mysql_query("SELECT * FROM table1 WHERE id1=5", $link); while ($row2=mysql_fetch_array($q2)) { //do some stuff with resultset...
1
3104
by: Pat | last post by:
Hi all, I have a really awkward situation that is causing memory leak problems. I'm passing data to a driver, and unfortunately, the driver code is not something I can change, and it is written in C, so it deals with the data as a big BYTE array. Basically, the driver expects a struct, followed immediately in memory by a big chunk of raw BYTE data. The size of the array of BYTEs is determined by certain members of the struct. So...
13
2464
by: programming | last post by:
how do i delete from a text file 1 of the following lines: jon|scott adam|smith <--delete paul|clark say i would like to delete the middle line of this txt, in member.txt what php code or logic would help me accomplish this?
3
3754
by: =?Utf-8?B?UmF5IE1pdGNoZWxs?= | last post by:
One more for today.... As I add more and more lines to my RichTextBox the array that holds its strings gets bigger and bigger and the vertical scroll bar gets smaller and smaller until the string array finally runs out of memory. I'd like to set some line limit and once that limit is reached, start removing the first line each time a new line is appended onto the end. I think this is probably more of an array manipulation question than...
9
1685
by: n00b | last post by:
Hello everyone, I just had a question about deleting objects in C#. For example if I have a string array and I wanted to make it grow by one size adding the string "dd" to it. string array = new string {"aa", "bb", "cc"}; string temp = array; array = new string; array = "dd";
3
3927
by: nigelesquire | last post by:
Please help! I'm trying to clone and delete multiple rows with JavaScript. I need two delete buttons that work...! I only have one for now, but it's not working properly, the output count is messing up. Problems:
0
8991
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8830
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9541
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
9321
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
6074
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4602
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4874
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3312
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2782
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.