Let's say that I have vector
of pairs, where each pair
corresponds to indexes (row and column) of certain matrix I am working on
using namespace std;
vector<pair<int, int>> vec;
I wanted to, using auto
, go through the whole vector and delete at once all the pairs that fulfill certain conditions, for example something like
for (auto& x : vec) {
if (x.first == x.second) {
vec.erase(x);
}
}
but it doesn't work, as I suppose vec.erase()
should have an iterator as an argument and x
is actually a pair
that is an element of vector vec
, not iterator. I tried to modify it in few ways, but I am not sure how going through container elements with auto
exactly works and how can I fix this.
Can I easily modify the code above to make it work and to erase multiple elements of vector, while going through it with auto
? Or I should modify my approach?
For now it's just a vector of pairs, but it will be much worse later on, so I would like to use auto
for simplicity.