Reputation: 79
I have these following variables:
vector< unordered_map< char, set<int> > > LNFA, NFA;
vector< set<int> > L_enclosure;
set<char> characters;
int nr_states;
set<int> acceptance_states, NFA_accept;
And I tried using this function to remove a certain number from std::set from NFA variable. But if i print the content in it it display even the items that i want to delete. These variables are globally declared.
This is the function:
void remove_state(int x) {
for (int i = 0; i < nr_states; ++i) {
for (auto jt : NFA[i]) {
jt.second.erase(x);
}
}
}
What am i possibly doing wrong to not receive the desired output?
Upvotes: 1
Views: 1543
Reputation: 118300
for (auto jt : NFA[i]) {
jt.second.erase(x);
The range iteration here iterates by value. You need to iterate by reference:
for (auto &jt : NFA[i]) {
jt.second.erase(x);
Your original range iteration was logically equivalent to the following (not quite, actually, but the minutiae differences are not relevant for the purpose of this question):
for (auto b=NFA[i].begin(); b != NFA[i].end(); ++b)
{
auto jt=*b;
jt.erase(x);
}
So you end up erasing the value from a copy of the actual std::set
. Not a useful result.
You need to make your range iteration use a reference, so that your range iteration becomes logically equivalent to:
auto &jt=*b;
Upvotes: 5