-
erase in vector
How do you erase something in a vector? My book tells me to use pop_back, but from what I've been reading and trying out it removes the last object of my vector. I've tried erase, but I can't make it work!
Also, once this does work, will my if statement be ok if I add it to the code?
Any help is appreciated as always.
Code:
for(i=0; i<=Process.size()-1; i++)
{
if (lastName == Access[i].lastName)
// &&
// Access[i].firstName == firstName &&
// Access[i].middleIni == middleIni)
{
Access.erase(i);
}
-
-
I've tried that but it doesn't work.
erase can not be used on it's own, at least not in my code.
I've tried Access.erase() and Access.erase(i) (which is what I want to do, is erase i) but I get a compiler error.
I don't get it though. When I tried Access.erase(0) just to see what parameter it would accept, it worked. But I'm obviously looking for a specific subscript, not the first one. Since 'i' evaluates to an integer just as 0 does, why doesn't it take i?
I don't know where to look for answers anymore. I've looked in my books, on this site, on the web.....I am totally stumped. I know I ask a lot of questions, but I do try very hard before I post and I've got to tell you, this place is one of the best place to learn in my opinion.
-
Oh, and I've tried what my book says, which is
Access[i] = Access[i+1];
Access.pop_back();
it seemed to work at times, but it mostly doubles my last entry and doesn't always delete what it's supposed to delete.
Again, any comments and suggestions are welcome.
-
Okay, as far as I know pop_back() will remove information in first in last out order. So you can't just delete an arbitrary value. Here is a sample of erase.
Code:
vector<int> array;
// add some stuff to the vector
array.push_back(1);
array.push_back(2);
array.push_back(3);
array.push_back(4);
array.push_back(5);
array.push_back(6);
array.push_back(7);
array.push_back(8);
array.push_back(9);
array.push_back(10);
array.erase(array.begin() + 6);
[edit]
I deleted the 6th entry.
[/edit]
-
Thanks for your suggestion Master, it's getting me closer.... but it's still doesn't work.... I don't mean to be a pest, but you seem to know how this works...
Here is the whole code segment. Thing is, I'm looking for the element to erase and then trying to erase it.... maybe that's the problem? It has got to erase i.... so I've tried it this way...
I get an assertion fault when I try to run it. It compiles fine on VC++.
[code]
void Delete(vector<Names>& Access, vector<Entry>& Process, int& i)
// Pre condition: Vector size is known
// Post condition: Deletes an entry
{
string firstName;
string lastName;
string middleIni;
cout << "\nYou have chosen to delete an entry.\n\n";
cout << "Please enter the first name: ";
cin >> firstName;
cout << "\n\nPlease enter the middle initial: ";
cin >> middleIni;
cout << "\n\nPlease enter the last name: ";
cin >> lastName;
for(i=0; i<=Process.size()-1; i++)
{
cout << lastName << Access[i].lastName;
if (lastName == Access[i].lastName)
// && Access[i].firstName == firstName &&
// Access[i].middleIni == middleIni)
{
cout << "BOO";
}
}
Access.erase(Access.begin() + i);
Menu(Access, Process, i);
} // End Delete
-
Why are you passing in i as a parameter (let alone why is it being passed by reference)? Also, shouldn't
Code:
Access.erase(Access.begin() + i);
Menu(Access, Process, i);
be inside the for loop? I believe thi s is the problem.
-
It just occured to me your problem is that you should have a break statement inside the if statement. Everything I see looks fine (within this context). Your problem may be in another function.
-
I've put it inside the loop.
Well, I've been trying for hours! I'm just plain out of ideas.....
The break statement doesn't seem to do anything. Whether it's there or not it acts the same.
The
Access.erase(Access.begin() + i);
compiles, but it doesn't erase anything.
The only way I got any deletion was with pop_back, which I've tried again, and I got it to delete in the middle of the vector, but with very weird results.
If anyone can help so I can stop pulling my hair out, I'd appreciate it.
-
Use a combination of erase and remove......
Code:
vector<int> vec;
.
.
.
vec.erase(remove(vec.begin(),vec.end(),ValToBeErased),vec.end());
-
How do you call this function from somewhere else? What I am trying to get at is what would you put into the third parameter? I would think it would work like so:
Code:
void Delete(vector<Names>& Access, vector<Entry>& Process)
// Pre condition: Vector size is known
// Post condition: Deletes an entry
{
string firstName;
string lastName;
string middleIni;
cout << "\nYou have chosen to delete an entry.\n\n";
cout << "Please enter the first name: ";
cin >> firstName;
cout << "\n\nPlease enter the middle initial: ";
cin >> middleIni;
cout << "\n\nPlease enter the last name: ";
cin >> lastName;
for(int i=0; i<=Process.size()-1; i++)
{
if (lastName == Access[i].lastName && Access[i].firstName == firstName && Access[i].middleIni == middleIni)
{
Access.erase(Access.begin() + i);
Process.erase(Process.begin()+i); // not sure if you wanted to delete process as well
break;
}
}
Menu(Access, Process, i);
} // End Delete
Let me know if this works
-
Master that third parameter shouldn't of been there, that was my bad.
Thanks , Stoned I'll keep that snippet in mind, except that it told me that
Code:
vec.erase(remove(vec.begin(),vec.end(),ValToBeErased),vec.end());
vec.erase didn't take 3 parameters.
Thanks PJ! Of course it worked :) I truly appreciate it.
And yes, I'm calling the function from a menu function.
I'm almost done with the whole thing, it works ok, but it's not as robust as I would like it to be..... but I think I've passed the major hurdles. It's not very elegant, but it will do.
Thanks everyone!!
-
I see only 2 params. maybe you typed it wrongly.
param1= the return value of remove.
param2= the iterator returned by end()
-
:eek:Sorry Stoned, my bad.... it was remove that didn't accept 3 parameters... I appreciate your comments... Like the name too :)
-
did u #include<algorithm>?