Let’s say I have my data in a 4 by 3 vector<vector<int> > as:
1 2 3
4 5 6
7 8 9
10 11 12
and I want to delete every row containing the element 8 and end up like:
1 2 3
4 5 6
10 11 12
I have been trying to do:
for (vector<vector<int> >::iterator it = v.begin(); it != v.end(); it++) {
if (find(it->begin(), it->end(), 8)) {
// I will remove the row in here
}
}
which gives me:
error: no matching function for call to 'find(std::vector<int>::iterator, std::vector<int>::iterator, int)'
I don’t have much experience with stl so I was wondering:
- what’s wrong with my
findcall? - is it safe to remove an element from a vector while iterating over it?
Ofc any elegant solution to my problem is also welcomed..
You probably forgot to
#include <algorithm>Look into the erase-remove idiom – erasing vector elements can be tricky.