| | |
STL vector - deleting the last element
Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved |
•
•
Join Date: May 2007
Posts: 15
Reputation:
Solved Threads: 2
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:
Any idea what I'm doing wrong? Thanks!
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:
C++ Syntax (Toggle Plain Text)
vector<course> cache; //holds objects in memory vector<course>::iterator point; //points at the vector //fill vector with data, etc etc cout << "Input course number to delete: "; cin >> target; point = cache.begin(); for(int n=0; point != cache.end(); point++, n++) { if(point->get_number() == target) { cache.erase(point); } //erases object }
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.
•
•
Join Date: May 2007
Posts: 15
Reputation:
Solved Threads: 2
However, my course class is composed as follows:
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)
C++ Syntax (Toggle Plain Text)
class course { private: string prefix; int number; string title; int credits; char grade; public: //Constructors course(); course(string, int, string, int, char); //Accessors void set(); void print() const; string get_prefix() const; int get_number() const; string get_title() const; int get_credits() const; char get_grade() const; //Operators bool operator < (course) const; bool operator > (course) const; bool operator == (course) const; bool operator != (course) const; };
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)
•
•
Join Date: Dec 2006
Posts: 1,089
Reputation:
Solved Threads: 164
C++ Syntax (Toggle Plain Text)
struct eq_course_number { explicit eq_course_number( int n ) : course_num(n) {} bool operator() ( const course& c ) const { return c.get_number() == course_num ; } int course_num ; }; // cin >> target; if( remove_if ( cache.begin(), cache.end(), eq_course_number(target) ) != cache.end() ) 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.
•
•
Join Date: Dec 2006
Posts: 1,089
Reputation:
Solved Threads: 164
•
•
•
•
C++ Syntax (Toggle Plain Text)
if( remove_if ( cache.begin(), cache.end(), eq_course_number(target) ) != cache.end() ) cache.pop_back() ;
C++ Syntax (Toggle Plain Text)
cache.erase( remove_if ( cache.begin(), cache.end(), eq_course_number(target) ), cache.end() ) ;
Last edited by vijayan121; May 3rd, 2007 at 3:24 pm.
•
•
Join Date: May 2007
Posts: 15
Reputation:
Solved Threads: 2
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
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
Could someone explain to me how the
C++ Syntax (Toggle Plain Text)
explicit eq_course_number( int n ) : course_num(n) {}
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
•
•
Join Date: May 2007
Posts: 15
Reputation:
Solved Threads: 2
Note: I also threw in an overloaded == operator for use with integers
And now Narue's method works!
C++ Syntax (Toggle Plain Text)
bool course::operator == (int n) const //returns based on class # {//overloaded for use with an integer if (number == n) { return true; } else { return false; } }
•
•
Join Date: Dec 2006
Posts: 1,089
Reputation:
Solved Threads: 164
•
•
•
•
Could someone explain to me how the
works?C++ Syntax (Toggle Plain Text)
explicit eq_course_number( int n ) : course_num(n) {}
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.
![]() |
Similar Threads
Other Threads in the C++ Forum
- Previous Thread: what this error mean ? Any 1 pleeese
- Next Thread: stack help
| Thread Tools | Search this Thread |
api array arrays based binary bitmap c++ c/c++ calculator char char* class classes code coding compile compiler console conversion convert count data database delete deploy developer dll download dynamic dynamiccharacterarray email encryption error file forms fstream function functions game generator getline givemetehcodez graph gui homeworkhelp iamthwee ifstream input int java lib linkedlist linker list loop looping loops map math matrix memory multiple news node number numbertoword output pointer problem program programming project python random read recursion recursive reference rpg sorting string strings temperature template test text text-file tree url variable vector video visual visualstudio win32 windows winsock word wordfrequency wxwidgets






