mercredi 1 mai 2019

Unordered_map acts strange when I add a new element

This lines are not working properly:

for (auto prod : productions_[*productionNonterm])
                productions_[nonterminal].push_back(prod);

If productions_[*productionNonterm] has only 1 element, everything is good. But if it has at least 2 elements, productionNonterm is modified and I have no idea why.

vector<string> nonterminals_;
unordered_map<string, vector<string>> productions_;

for (const auto &nonterminal : nonterminals_) {
    for (auto productionNonterm = productions_[nonterminal].begin(); productionNonterm != productions_[nonterminal].end(); ++productionNonterm) {
        if (cntNonterminalsInProduction(*productionNonterm) == 1 && cntTerminalsInProduction(*productionNonterm) == 0) {
            nonterminals_.erase(find(nonterminals_.begin(), nonterminals_.end(), *productionNonterm));

            for (auto prod : productions_[*productionNonterm])
                productions_[nonterminal].push_back(prod);

            productions_[*productionNonterm].erase(productions_[*productionNonterm].begin(), productions_[*productionNonterm].end());

            productions_[nonterminal].erase(productionNonterm);
            --productionNonterm;

        }
    }
}

Aucun commentaire:

Enregistrer un commentaire