Deleting some specified element from the map (delete method)
At times, there might be some situations, when we need to delete some element from the map object. In such situations, we can make use of the delete method. Here, we need to pass the key, which we want to delete. If the key-value pair was deleted, the output comes out to be true, and else the output is going to be false.
Let’s have a look at an example, which demonstrates the use of the delete method, for deleting some elements from the map object –
In the above program, we can see that we are trying to delete the element with key 2. Since we have the element with the key 2, the element would be deleted, and the value returned as true. Let’s have a look at the output now –
As you can see, since the key existed, it was deleted, and it returned true because the key and its value were removed. If we try to give some key, for which, the element does not exist, we are going to get the output as false. You can try doing this though.