STL vector - deleting the last element

Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved

Join Date: May 2007
Posts: 15
Reputation: yesm is an unknown quantity at this point 
Solved Threads: 2
yesm yesm is offline Offline
Newbie Poster

STL vector - deleting the last element

 
0
  #1
May 3rd, 2007
Hello! I'm working on an assignment that requires me to store custom classes in an STL vector and do things like search, display, and delete them, then write them all to file.

This is going well, but my code to delete an object (adapted from this thread) works on any object EXCEPT the last object; it crashes whenever I try to delete the last one in the chain.

Here is my code for the deletion:

  1. vector<course> cache; //holds objects in memory
  2. vector<course>::iterator point; //points at the vector
  3.  
  4.  
  5. //fill vector with data, etc etc
  6.  
  7.  
  8. cout << "Input course number to delete: ";
  9. cin >> target;
  10. point = cache.begin();
  11.  
  12. for(int n=0; point != cache.end(); point++, n++) {
  13. if(point->get_number() == target)
  14. { cache.erase(point); } //erases object
  15. }
Any idea what I'm doing wrong? Thanks!
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,740
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 739
Team Colleague
Narue's Avatar
Narue Narue is online now Online
Code Goddess

Re: STL vector - deleting the last element

 
0
  #2
May 3rd, 2007
Eh, a general rule of thumb is not to loop with iterators if you're going to modify the container. The good news is that the algorithm header has a function template that you can use to remove all occurrences of an item:
#include <algorithm>

cout << "Input course number to delete: ";
cin >> target;

cache.erase ( remove ( cache.begin(), cache.end(), target ), cache.end() );
Last edited by Narue; May 3rd, 2007 at 2:16 pm.
I'm here to prove you wrong.
Reply With Quote Quick reply to this message  
Join Date: May 2007
Posts: 15
Reputation: yesm is an unknown quantity at this point 
Solved Threads: 2
yesm yesm is offline Offline
Newbie Poster

Re: STL vector - deleting the last element

 
0
  #3
May 3rd, 2007
However, my course class is composed as follows:

  1. class course {
  2. private:
  3. string prefix;
  4. int number;
  5. string title;
  6. int credits;
  7. char grade;
  8. public:
  9. //Constructors
  10. course();
  11. course(string, int, string, int, char);
  12. //Accessors
  13. void set();
  14. void print() const;
  15. string get_prefix() const;
  16. int get_number() const;
  17. string get_title() const;
  18. int get_credits() const;
  19. char get_grade() const;
  20. //Operators
  21. bool operator < (course) const;
  22. bool operator > (course) const;
  23. bool operator == (course) const;
  24. bool operator != (course) const;
  25. };

Wouldn't that code erase an object any time it found target in the course number or credits, since both are ints?

(sorry for the dumb questions XD)
Reply With Quote Quick reply to this message  
Join Date: May 2007
Posts: 15
Reputation: yesm is an unknown quantity at this point 
Solved Threads: 2
yesm yesm is offline Offline
Newbie Poster

Re: STL vector - deleting the last element

 
0
  #4
May 3rd, 2007
Also, I got a ton of errors when I slap-dashed that into my code.
(and yes, I have included the algorithm library)
Attached Thumbnails
errors.jpg  
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,740
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 739
Team Colleague
Narue's Avatar
Narue Narue is online now Online
Code Goddess

Re: STL vector - deleting the last element

 
0
  #5
May 3rd, 2007
Sorry, I didn't notice you were using custom objects. The easiest solution would be to do some research on remove_if. That function allows you to pass a predicate for custom comparisons. It would probably be easier than modifying your class to remove those errors.
I'm here to prove you wrong.
Reply With Quote Quick reply to this message  
Join Date: Dec 2006
Posts: 1,089
Reputation: vijayan121 is a name known to all vijayan121 is a name known to all vijayan121 is a name known to all vijayan121 is a name known to all vijayan121 is a name known to all vijayan121 is a name known to all 
Solved Threads: 164
vijayan121 vijayan121 is offline Offline
Veteran Poster

Re: STL vector - deleting the last element

 
0
  #6
May 3rd, 2007
  1. struct eq_course_number
  2. {
  3. explicit eq_course_number( int n ) : course_num(n) {}
  4. bool operator() ( const course& c ) const
  5. { return c.get_number() == course_num ; }
  6. int course_num ;
  7. };
  8.  
  9. //
  10. cin >> target;
  11. if( remove_if ( cache.begin(), cache.end(),
  12. eq_course_number(target) ) != cache.end() )
  13. cache.pop_back() ;

an alternative is to overload operator== to test for equivalence between course and an integer course_number. then remove could be used (as in Narue's post)
Last edited by vijayan121; May 3rd, 2007 at 2:56 pm.
Reply With Quote Quick reply to this message  
Join Date: Dec 2006
Posts: 1,089
Reputation: vijayan121 is a name known to all vijayan121 is a name known to all vijayan121 is a name known to all vijayan121 is a name known to all vijayan121 is a name known to all vijayan121 is a name known to all 
Solved Threads: 164
vijayan121 vijayan121 is offline Offline
Veteran Poster

Re: STL vector - deleting the last element

 
0
  #7
May 3rd, 2007
Originally Posted by vijayan121 View Post
  1. if( remove_if ( cache.begin(), cache.end(),
  2. eq_course_number(target) ) != cache.end() )
  3. cache.pop_back() ;
this will remove only one object; i'd assumed that courses in the cache would be unique. if you want to remove many elements which have the same course number, it needs to be written as Narue had suggested.
  1. cache.erase( remove_if ( cache.begin(), cache.end(),
  2. eq_course_number(target) ), cache.end() ) ;
this would also work if there is just one element or even none at all.
Last edited by vijayan121; May 3rd, 2007 at 3:24 pm.
Reply With Quote Quick reply to this message  
Join Date: May 2007
Posts: 15
Reputation: yesm is an unknown quantity at this point 
Solved Threads: 2
yesm yesm is offline Offline
Newbie Poster

Re: STL vector - deleting the last element

 
0
  #8
May 3rd, 2007
The object (to hold college course data) would probably only contain one object of a given course number, since it would be unlikely to be enrolled in two identical college courses in the same semester.

Could someone explain to me how the
  1. explicit eq_course_number( int n ) : course_num(n) {}
works? I'm farmiliar with using class objects and the () operator to get stuff done, but I'm having trouble passing extra parameters to functions like he does.

EDIT: And, I thought pop_back() will always remove the LAST element in a vector.
The code still works, I'm just a tad confused on how it does so
Last edited by yesm; May 3rd, 2007 at 3:37 pm. Reason: Added another question
Reply With Quote Quick reply to this message  
Join Date: May 2007
Posts: 15
Reputation: yesm is an unknown quantity at this point 
Solved Threads: 2
yesm yesm is offline Offline
Newbie Poster

Re: STL vector - deleting the last element

 
0
  #9
May 3rd, 2007
Note: I also threw in an overloaded == operator for use with integers
  1. bool course::operator == (int n) const //returns based on class #
  2. {//overloaded for use with an integer
  3. if (number == n) {
  4. return true;
  5. } else {
  6. return false;
  7. }
  8. }
And now Narue's method works!
Reply With Quote Quick reply to this message  
Join Date: Dec 2006
Posts: 1,089
Reputation: vijayan121 is a name known to all vijayan121 is a name known to all vijayan121 is a name known to all vijayan121 is a name known to all vijayan121 is a name known to all vijayan121 is a name known to all 
Solved Threads: 164
vijayan121 vijayan121 is offline Offline
Veteran Poster

Re: STL vector - deleting the last element

 
0
  #10
May 3rd, 2007
Originally Posted by yesm View Post
Could someone explain to me how the
  1. explicit eq_course_number( int n ) : course_num(n) {}
works?
eq_course_number is a class and the line you are referring to is the constructor. logically, the == operation is a binary one; the remove_if requires a unary predicate. so we have an object which stores the extra information in a member variable and uses it as the second parameter when the function ( operator() ) is called with just one parameter. in c++ jargon, this is called binding an argument.

the explicit keyword disables an implicit conversion from an int to an object of type eq_course_number. it does not affect anything else.
Last edited by vijayan121; May 3rd, 2007 at 3:42 pm.
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the C++ Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC