Dec 23, 2016 at 1:27pm Dec 23, 2016 at 1:27pm UTC
Hi,
I'm still working on my list. Now I'm trying to delete a person. It works fine except when I write the out the list I suddenly have a row with just a length, no first name or last name. And not the length of the just erased person but from someone who is currently in the list.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
struct Person
{
string firstname;
string lastname;
float length;
};
void erasePerson(vector<Person>& persons)
{
string erase;
cout << "Enter the signature of the person you want to delete from the list: " ;
cin >> erase;
auto pred = [erase]( Person & item)
{
return item.sign == erase;
};
auto itr = std::remove_if(std::begin(persons), std::end(persons), pred);
if ( itr!= std::end(persons))
{
for (auto & e : persons)
{
cout << right << setw(20) << e.firstname;
cout << setw(20) << e.lastname;
cout << right << setw(20) << e.length << endl;
}
}
else
{
cout << "Person missing \n" ;
}
}
Last edited on Dec 23, 2016 at 1:27pm Dec 23, 2016 at 1:27pm UTC
Dec 26, 2016 at 8:53pm Dec 26, 2016 at 8:53pm UTC
`remove_if()' returns you an iterator, you need to `.erase()' from that iterator to `.end()'