1

So I am attempting to delete all of the variables of a struct from a list, my struct is as so

struct LoginDetails
{
public:
    string userName;
    string userFirstName;
    string userLastName;
    string password;
    string tutor;
    int userAccess;
};

How do I add to the following code to then remove all of what is in the struct.

cout << "Please enter a username to upgrade the account.\n";
cin >> username;
for (list<LoginDetails>::iterator itri = loginDetails->begin(); itri != loginDetails->end(); itri++)
{
    if (itri->userName == username)
    {

    }
}
bilbsy
  • 67
  • 1
  • 10

2 Answers2

0

You can do something like this:-

   for (list<LoginDetails>::iterator itri = loginDetails->begin(); itri != loginDetails->end(); )
   {
      if (itri->userName == username)
      {
         itri =  loginDetails->erase(itri);               
      }
      else
         itri++;
   }

But remember for list you can erase an element without affecting the validity to any iterators to other elements. Other containers like vector or deque are not so kind. For those containers only elements before the erased iterator remain untouched. This difference is simply because lists store elements in individually allocated nodes. It's easy to take one link out. vectors are contiguous, taking one element out moves all elements after it back one position.

ravi
  • 10,994
  • 1
  • 18
  • 36
0

Looks like you want to erase items from list, you can do like following :

 list<LoginDetails>::iterator itri = loginDetails->begin(); 
 while (  itri != loginDetails->end() )
 {
    if (itri->userName == username)
    {
        loginDetails->erase(itri++);
    }

    else
    {
        ++itri;
    }
  }
P0W
  • 46,614
  • 9
  • 72
  • 119