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
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:
- How do I delete my old Yahoo page after I made a new page?Best solution by answers.yahoo.com
- How do I delete favorites from my list?Best solution by Yahoo! Answers
- How do I delete some email addresses from my contacts list?Best solution by help.madmimi.com
- How can I delete my friends in my friend list?Best solution by Yahoo! Answers
- How do I delete a comment that I made on a video in YouTube?Best solution by ChaCha
Just Added Q & A:
- How many active mobile subscribers are there in China?Best solution by Quora
- How to find the right vacation?Best solution by bookit.com
- How To Make Your Own Primer?Best solution by thekrazycouponlady.com
- How do you get the domain & range?Best solution by ChaCha
- How do you open pop up blockers?Best solution by Yahoo! Answers
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.