Hi;
I was confused on how to go about deleting an element from an array at a specific index. I would like the size to reflect this and not have an empty space. I'm kind of confused on how to do this. Thanks
I"m not sure what your mean. I have a structure the array is of that type. I haven't learned stl:: vector
I was thinking about using push or pop I forget which one will remove. But I wasn't sure if I can use it with the array. In my book it was for vectors. Does that mean its strictly for vectors?
If you use std::vector, you can call the erase() method, will do what you want. If you use an array, you'll have to flag the element to be deleted, and then copy the array over to a new one minus the flagged yourself.
Push/pop applies to stacks, that's not what you want. It can be done, but without random access it's kind of annoying.
Not really any need of an iterator, though. It'll get invalidated every time erase() is called. I assume OP is looking for a specific value in the vector to delete, just using indices would be much easier.
Well it all depends on what your needs are. If you are storing a large amount of data and quite often need to get a specific pointer out of the container, you would be better off using a std::map as you store each object with a key and can then retrieve each item required by doing a find with the key. The find then returns an iterator from which you can get your pointer.
I was confused on how to go about deleting an element from an array at a specific index. I would like the size to reflect this and not have an empty space. I'm kind of confused on how to do this. Thanks
| 0 | 1 | 2 | 3 | 4 | 5 |
If this is an array, and you want to remove the element at index 3, you move the element at index 4 left one, then you move the element at level 5 left one.
If the relative order of the array isn't important, then you can simplify things by swapping the element you want to move with the last element in the array, and decrementing the size.
Yeah I want to use indices. I have name address, zip, phone at some given index because I'm using a structure. So I want o just delete it and clean up my array so to speak so I don't have the extra space. That's all I'm looking for. Hey what is OP I don't know what that is?
So the easiest way is just to move it to another array minus the one that's gone. What would that look like to keep that one in the old one and move everything else?Thanks
I'm sorry thanks for writing that snipet of code I'm having a difficult time with this.
If the relative order of the array isn't important, then you can simplify things by swapping the element you want to move with the last element in the array, and decrementing the size.
The relative order is not important at this point. I have to display this in alphabetical order by last name at some point. If I had a sort function couldn't I just pass it to that after it has been ordered? Moving them one by one is kind of confusing So if I have an array of 10 and I want to remove the element at position 3 and the array goes 0,1,2,3,4...... So the element to right of 3 must be moved to the left one? and this will in effect slide element 3 down the list to the end? Where I can....?
Thanks
So if I have an array of 10 and I want to remove the element at position 3 and the array goes 0,1,2,3,4...... So the element to right of 3 must be moved to the left one? and this will in effect slide element 3 down the list to the end?
No. As soon as you move the element to the right of 3, 3 is overwitten.
If I have: | 0 | 1 | 2 | 3 | 4 | 5 |
and I remove the 2, the resulting array looks like: | 0 | 1 | 3 | 3 | 4 | 5 | -> 3rd element = 4th element | 0 | 1 | 3 | 4 | 4 | 5 | -> 4th element = 5th element | 0 | 1 | 3 | 4 | 5 | 5 | -> 5th element = 6th element
Now we decrement the variable holding the number of values in the array so it is effectively: | 0 | 1 | 3 | 4 | 5 |
Now:
If the relative order of the array isn't important, then you can simplify things by swapping the element you want to move with the last element in the array, and decrementing the size.
If we have | 0 | 1 | 2 | 3 | 4 | 5 |
and we want to remove the 2, we swap it with the last element: | 0 | 1 | 5 | 3 | 4 | 2 |
And then we decrement the size, effectively giving us: | 0 | 1 | 5 | 3 | 4 |
As you can see the order of the elements is not preserved.
If I have:
| 0 | 1 | 2 | 3 | 4 | 5 |
and I remove the 2, the resulting array looks like:
| 0 | 1 | 3 | 3 | 4 | 5 | -> 3rd element = 4th element
| 0 | 1 | 3 | 4 | 4 | 5 | -> 4th element = 5th element
| 0 | 1 | 3 | 4 | 5 | 5 | -> 5th element = 6th element
Now we decrement the variable holding the number of values in the array so it is effectively:
| 0 | 1 | 3 | 4 | 5 |
I understand this from your diagrams (very nice explanation). I have a hard time expressing this as code. I don't even know how to express that. And when I think of it in relation to my problem it becomes more complicated because I'm using the structure I believe. More specifically.
//Option D function
void optionD( ifstream &fin, infoType tele[], int& size)
{
int index = 0;
string dPhone;
bool found = false;
string temp = "";
cout << "You have selected option D " << endl;
cout << "Please enter the phone number of the record to be deleted." << endl;
cout << "(Example, 555-555-5555)" << endl;
cin >> dPhone;
while( index < size && !found )
{
if(tele[index].phone.compare(dPhone)== 0)
{
cout << "The entry to be deleted is " << endl;
cout << tele[index].lname << endl;
cout << tele[index].fname << endl;
cout << tele[index].streetAdd << endl;
cout << tele[index].cityStateZ << endl;
cout << tele[index].phone << endl;
found = true;
}
index++;
}
}
So this is where I can't seem to get my thoughts together. I want to know after this last bit of code with the loop implement what you have told me. So if I have that loop and I have located the correct phone number how can I use that location to do your idea. When I would have to do it outside of the that chunck of code. After the loop. How do I accomplish this. How do I manage the issue with location and how do I move the location of that index like you have told me.
Thanks for the good explanation that is clear but not the expression in code
I don't need 2 arrays to do the trick you suggest?
//Remove function
int remove( infoType tele[], int& size, int position_of_item_to_remove )
{
for( int i = position_of_item_to_remove + 1 ; i < size ; ++i )
tele[i] = tele[i-1] ;
return --size;
}
//Option D function
void optionD( ifstream &fin, infoType tele[], int& size)
{
int index = 0;
string dPhone;
bool found = false;
string temp = "";
cout << "You have selected option D " << endl;
cout << "Please enter the phone number of the record to be deleted." << endl;
cout << "(Example, 555-555-5555)" << endl;
cin >> dPhone;
while( index < size && !found )
{
if(tele[index].phone.compare(dPhone)== 0)
{
cout << "The entry to be deleted is " << endl;
cout << tele[index].lname << endl;
cout << tele[index].fname << endl;
cout << tele[index].streetAdd << endl;
cout << tele[index].cityStateZ << endl;
cout << tele[index].phone << endl;
//Remove
int new_size = remove( tele.size, index );
found = true;
}
else
index++;
}
}
But I get errors .
----- Build started: Project: newnew, Configuration: Debug Win32 ------
1> newnew.cpp
1>c:\users\jordan\documents\visual studio 2010\projects\whatshesent\whatshesent\newnew.cpp(126): error C2228: left of '.size' must have class/struct/union
1> type is 'infoType []'
1> did you intend to use '->' instead?
1>c:\users\jordan\documents\visual studio 2010\projects\whatshesent\whatshesent\newnew.cpp(126): error C2661: 'remove' : no overloaded function takes 2 arguments
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
....
If my compiler had feelings it would hate me ha.
What is the class/struct/union about ?
Thx
Never mind it compiled but then it throws a crazy error.
Unhandled exception at 0x5c1dcafa (msvcr100d.dll) in newnew.exe: 0xC0000005: Access violation writing location 0xcccccccc.
I have no clue what this is. It compiles then throws this erros wtf?