I have a map in the following format -
map<int, vector<int>> myGraph;
I am trying to delete a key from myGraph using -
myGraph.erase(firstValue);
Here firstValue is key from myGraph.
The value of corresponding to firstValue key is getting deleted but the key still exists in the map.
int main(void) {
string name;
map<int, vector<int>> myGraph;
int temp, value;
while(getline(cin, name)) {
stringstream stream(name);
stream >> value;
while(stream >> temp) {
myGraph[value].push_back(temp);
}
}
while(myGraph.size() > 2) {
auto iter = myGraph.begin();
advance(iter, rand() % myGraph.size());
auto mik = iter->second.begin();
cout << iter->second.size() << " " << iter->first<< endl;
advance(mik, rand() % iter->second.size());
int firstValue = iter->first, secondValue = *mik;
myGraph[secondValue].insert(myGraph[secondValue].end(), iter->second.begin(), iter->second.end());
myGraph.erase(firstValue);
myGraph[secondValue].erase(remove(myGraph[secondValue].begin(), myGraph[secondValue].end(), firstValue), myGraph[secondValue].end());
for(auto iterate = myGraph.begin(); iterate != myGraph.end(); iterate++) {
replace(iterate->second.begin(), iterate->second.end(), firstValue, secondValue);
}
}
cout << myGraph.size() << endl;
return 0;
}
Aucun commentaire:
Enregistrer un commentaire