Open In App

Different ways to delete elements in std::map (erase() and clear())

Last Updated : 23 Dec, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

This article deals with the deletion part of Maps. We can delete elements in std::map using two functions

1. Using erase()

The erase() is used to erase the pair in the map mentioned in the argument, either its position, its value, or a range of numbers. We can use the erase function in the following ways:

A. Remove using the Key

erase (key)

Erases the key-value pair using the key mentioned in its argument. reorders the map after deletion. It returns the number of entries deleted. If non-existing keys is deleted, 0 is returned.

  • Time complexity: log(n) (n is size of the map)

B. Remove using Iterator

erase (iter)

Erases the pair at the position pointed by the iterator mentioned in its argument. 

  • Time complexity: log(1)

C. Remove Elements in a Range

erase (strt_iter, end_iter)

Erases the range of pairs starting from “strt_iter” to the “end_iter”.

  • Time complexity: O(k) (where k is the linear distance between strt_iter and end_iter i.e. the number of elements between the given range).

Example

C++




// C++ code to demonstrate the working of erase()
  
#include <iostream>
#include <map> // for map operations
using namespace std;
  
int main()
{
    // declaring map
    // of char and int
    map<char, int> mp;
  
    // declaring iterators
    map<char, int>::iterator it;
    map<char, int>::iterator it1;
    map<char, int>::iterator it2;
  
    // inserting values
    mp['a'] = 5;
    mp['b'] = 10;
    mp['c'] = 15;
    mp['d'] = 20;
    mp['e'] = 30;
  
    // printing initial map elements
    cout << "The initial map elements are : \n";
  
    for (it1 = mp.begin(); it1 != mp.end(); ++it1)
        cout << it1->first << "->" << it1->second << endl;
  
    it = mp.begin();
  
    cout << endl;
  
    // erasing element using iterator
    // erases 2nd element
    // 'b'
    ++it;
    mp.erase(it);
  
    // printing map elements after deletion
    cout << "The map elements after 1st deletion are : \n";
  
    for (it1 = mp.begin(); it1 != mp.end(); ++it1)
        cout << it1->first << "->" << it1->second << endl;
  
    cout << endl;
  
    // erasing element using value
    int c = mp.erase('c');
  
    // printing map elements after deletion
    cout << "The map elements after 2nd deletion are : \n";
  
    for (it1 = mp.begin(); it1 != mp.end(); ++it1)
        cout << it1->first << "->" << it1->second << endl;
  
    cout << "The number of elements deleted in 2nd "
            "deletion are : ";
    cout << c << endl;
  
    cout << endl;
  
    // erasing element using value
    // key not present
    int d = mp.erase('w');
  
    // printing map elements after deletion
    cout << "The map elements after 3rd deletion are : \n";
  
    for (it1 = mp.begin(); it1 != mp.end(); ++it1)
        cout << it1->first << "->" << it1->second << endl;
  
    cout << "The number of elements deleted in 3rd "
            "deletion are : ";
    cout << d << endl;
  
    cout << endl;
  
    ++it;
    ++it;
  
    // erasing element using range iterator
    // deletes "d" and "e" keys
    mp.erase(it, mp.end());
  
    // printing map elements 4th deletion
    cout << "The map elements after 4th deletion are : \n";
  
    for (it1 = mp.begin(); it1 != mp.end(); ++it1)
        cout << it1->first << "->" << it1->second << endl;
  
    cout << endl;
}


Output

The initial map elements are : 
a->5
b->10
c->15
d->20
e->30

The map elements after 1st deletion are : 
a->5
c->15
d->20
e->30

The map elements after 2nd deletion are : 
a->5
d->20
e->30
The number of elements deleted in 2nd deletion are : 1

The map elements after 3rd deletion are : 
a->5
d->20
e->30
The number of elements deleted in 3rd deletion are : 0

The map elements after 4th deletion are : 
a->5

2. Using clear()

The clear function clears all the elements present in the map. After this function is called, the size of map becomes 0.

Syntax

map_name.clear()

Example

CPP




// C++ code to demonstrate the working of clear()
  
#include <iostream>
#include <map> // for map operations
using namespace std;
  
int main()
{
    // declaring map
    // of char and int
    map<char, int> mp;
  
    // declaring iterator
    map<char, int>::iterator it;
  
    // inserting values
    mp['a'] = 5;
    mp['b'] = 10;
    mp['c'] = 15;
    mp['d'] = 20;
    mp['e'] = 30;
  
    // printing initial map elements
    cout << "The initial map elements are : \n";
    for (auto it1 = mp.begin(); it1 != mp.end(); ++it1)
        cout << it1->first << "->" << it1->second << endl;
  
    // using clear() to erase all elements in map
    mp.clear();
  
    // printing map elements after deletion
    cout << "The map elements after clearing all elements "
            "are : \n";
    for (auto it1 = mp.begin(); it1 != mp.end(); ++it1)
        cout << it1->first << "->" << it1->second << endl;
}


Output

The initial map elements are : 
a->5
b->10
c->15
d->20
e->30
The map elements after clearing all elements are : 


Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads