I know that erasing elements from an associative container inside a for loop invalidates it.
Is it the case when using a range based loop?
#include <iostream>
#include <unordered_map>
#include <set>
#include <algorithm>
#include <ranges>
struct A {
int value;
// other members...
};
int main() {
std::unordered_map<std::string, std::set<A>> myMap;
// Assuming you have populated the map and the set
// Object of type A
A a;
for (auto& [key, valueSet] : myMap) {
valueSet.erase(a);
if (valueSet.empty()) {
myMap.erase(key);
}
}
return 0;
}
And how can the compiler detects it.
I run -fsanitize=address,undefined
but gcc doesn't show any warning.