How can I remove an element in a repeated list?

How to I delete an element from a list? [C++]?

  • Answer:

    depends on the error message but i've just had a look at the std::list reference and it might be because you need to pass a const reference. void Person::deleteFriend(const Person &p) // require a constance reference { friends.remove(p); // pass that in } you should add tests in aswell such void Person::deleteFriend(const Person &p) { if (friends.size() == 0) { std::cout << "List is empty" << std::endl; return; } if (friends.find(p) == friend.end()) { std::cout << "Person not found" << std::endl; return; } friends.remove(p); } helps with debugging so you don't have to go straight to gdb. You could also add C preprocessor directives (or whatever they're called) have a global definition of a constant called _DEBUG or anything really and since we dont care what it holds only the fact that it exists, put this at the beginning of the header or a header that it depends on then we can toggle whether the safety checks get included in the final build .. #define _DEBUG .. void Person::deleteFriend(const Person &p) { #ifdef _DEBUG if (friends.size() == 0) { std::cout << "List is empty" << std::endl; return; } if (friends.find(p) == friend.end()) { std::cout << "Person not found" << std::endl; return; } #endif friends.remove(p); } doubt i answered the q properly but i hope i helped.

Cahlen at Yahoo! Answers Visit the source

Was this solution helpful to you?

Other answers

depends on the error message but i've just had a look at the std::list reference and it might be because you need to pass a const reference. void Person::deleteFriend(const Person &p) // require a constance reference { friends.remove(p); // pass that in } you should add tests in aswell such void Person::deleteFriend(const Person &p) { if (friends.size() == 0) { std::cout << "List is empty" << std::endl; return; } if (friends.find(p) == friend.end()) { std::cout << "Person not found" << std::endl; return; } friends.remove(p); } helps with debugging so you don't have to go straight to gdb. You could also add C preprocessor directives (or whatever they're called) have a global definition of a constant called _DEBUG or anything really and since we dont care what it holds only the fact that it exists, put this at the beginning of the header or a header that it depends on then we can toggle whether the safety checks get included in the final build .. #define _DEBUG .. void Person::deleteFriend(const Person &p) { #ifdef _DEBUG if (friends.size() == 0) { std::cout << "List is empty" << std::endl; return; } if (friends.find(p) == friend.end()) { std::cout << "Person not found" << std::endl; return; } #endif friends.remove(p); } doubt i answered the q properly but i hope i helped.

flood

Related Q & A:

Just Added Q & A:

Find solution

For every problem there is a solution! Proved by Solucija.

  • Got an issue and looking for advice?

  • Ask Solucija to search every corner of the Web for help.

  • Get workable solutions and helpful tips in a moment.

Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.