Deleting a person from a list, list, vector<struct>

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
http://www.cplusplus.com/reference/algorithm/remove_if/
The function cannot alter the properties of the object containing the range of elements
1
2
3
4
5
6
7
8
persons.erase(
   std::remove_if(
      std::begin(persons),
      std::end(persons),
      pred
   ),
   std::end(persons)
);



> return item.sign == erase;
there is no member `sign' in `Person'
Forgot the sign in struct, sorry for that.
1
2
3
4
5
6
7
struct Person
{
    string firstname;
    string lastname;
    string sign;
    float length;
};

So I can't use remove_if in my case, or am I understanding wrong?
`remove_if()' returns you an iterator, you need to `.erase()' from that iterator to `.end()'
Topic archived. No new replies allowed.