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) { } } 
5
  • 1
    It's unclear what you mean by delete. Do you mean erase? Commented Nov 2, 2014 at 16:08
  • Please clarify! What do you mean with "remove all of what is in the struct." Resetting to default constructor values? Commented Nov 2, 2014 at 16:08
  • I want to be able to remove individual structs from the list. Commented Nov 2, 2014 at 16:09
  • @user2249719 std::list::erase() then. Commented Nov 2, 2014 at 16:10
  • Consider cplusplus.com/reference/list/list/remove_if Commented Nov 2, 2014 at 16:13

2 Answers 2

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; } } 
Sign up to request clarification or add additional context in comments.

Comments

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.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.