in the member function, i dont really care what the position is i just want to get the index of the iterator as to delete it as i have already "found" the object in the vector that i want to delete by the if condition.
The problem is not deleting it, the error i get is the display() member function. apparently I am sending in the iterator of the vector and not the actual object in the vector which the member function display() is set to receive as an argument? I think i might of got that right?
If i did, how do I send in the object and not the iterator?
So instead of overloading the member function display() to take in an iterator, how can i adapt it to just the object of the vector?
1 2 3 4 5 6 7 8
void Control::kill_old_update(){
for (std::vector<Bunny>::iterator it = bunny_obj_list.begin(); it != bunny_obj_list.end(); ++it){
if(it->age > age_to_die){
display(it, "old");
bunny_obj_list.erase(it);
}
}
}
error: /home/metulburr/Documents/cplusplus/radioactive_bunnies/src/Control.cpp|73|error: no matching function for call to ‘Control::display(std::vector<Bunny>::iterator&, constchar [4])’|
To call display with a Bunny as the first argument, just call display(*it, "old"):
However, keep in mind that the loop in kill_old_update() won't work as written: the call to erase() invalidates the iterator. Control::kill_old_update() should call remove_if() or some other algorithm since you want to do things to the old bunnies first (partition perhaps) followed by erase(), it has no business dealing with iterators on its own.
(edit: yes, using the return value of vector::erase() would make this work too)
just as i think i am starting to get the hang of it...ugh. lol they do not lie when they say c++ is harder than python, so to speak.
the call to erase() invalidates the iterator
im not sure what you mean by that.
I thought it was as follows:
//loop through the indexes of the vector
//if index.age > age_to_die
//display(index, "some_string")
//vector.erase(index)
1 2 3 4
for (std::vector<Bunny>::iterator it = bunny_obj_list.begin(); it != bunny_obj_list.end(); ++it){
if(it->age > age_to_die){
display(it, "old");
bunny_obj_list.erase(it);
(partition perhaps)
I also am not sure what you mean by partition?
since you want to do things to the old bunnies first
so this would be the proper method if i was just deleting the object and not doing anyhting with it within the for loop?
The C++ function std::partition(), you could use it to split the bunnies into old and not so old
would this matter only if i was deleting the vector objects by age, because i plan on making other member functions which will delete the same vector of objects by bool RA value, by age, by population number, by a number of other things too. Which would organizing them into a partition ordered by age moot?
When a C++ programmer encounters x.erase(remove_if(x.begin(), x.end(), condition), x.end()), this is instantly clear what's going on (every element for which the condition is true is erased).
The loop is more-or-less recognizable too (after vlad's correction of course), but it it has more moving parts to look at and it's a whole lot slower.
There are a few different ways to write the condition for remove_if, of course, classic (1998) approach was to write something like
I am still trying to convert myself from Python to c++, and i get lost in the syntax of C++ at times. Plus there are so many methods in the reference of each library, its hard to remember them all, well for me its hard to remember a few.