I am using mymap.find(key)==mymap.end
to see if key
is present in my map or not.
The value of this expression is returning true even when the key is not present
map is being changed from <key:1, value: node foo> to <key 2: value: Null>
Is there a reason behind this behaviour?
here’s the whole code
void put(int key, int value) {
Node* temp;
cout << "nnTrynna put " << key << " & " << value;
cout << "nbefore:";
printMap();
if (start == NULL) {
// some if else conditions
.
.
.
} else if (mymap.find(key) != mymap.end()) { // going into if case even if the key doesn't exist in map
cout << "-> case found";
cout << "nafter:";
printMap();
mymap[key]->data = value;
mymap[key]->prev->next = mymap[key]->next;
mymap[key]->next->prev = mymap[key]->prev;
end->next = mymap[key];
mymap[key]->prev = end;
end = mymap[key];
} else {
cout << "-> case not found ";
if (cap == 0) {
mymap.erase(start->key);
temp = start;
start = start->next;
if (start != NULL)
start->prev = NULL;
delete temp;
cap++;
}
if (start == NULL) {
start = end = new Node(key, value);
mymap[key] = end;
cap--;
} else {
end->next = new Node(key, value, end);
end = end->next;
mymap[key] = end;
cap--;
}
}
cout << "nafter:";
printMap();
printData();
}