VOOZH about

URL: https://www.geeksforgeeks.org/cpp/multimaperase-in-c-stl/

⇱ multimap::erase() in C++ STL - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

multimap::erase() in C++ STL

Last Updated : 18 Nov, 2020
    multimap::erase() is a built-in function in C++ STL which is used to erase element from the container. It can be used to erase keys, elements at any specified position or a given range.
  1. Syntax for erasing a key:
    multimap_name.erase(key)
    
    Parameters: The function accepts one mandatory parameter key which specifies the key to be erased in the multimap container. Return Value: The function does not return anything. It erases all the elements with the specified key.
    Output:
    The multimap before using erase() is : 
    KEY ELEMENT
    1 40
    2 30
    2 20
    3 60
    5 50
    
    The multimap after applying erase() is : 
    KEY ELEMENT
    5 50
    3 60
    
  2. Syntax for removing a position:
    multimap_name.erase(iterator position)
    
    Parameters: The function accept one mandatory parameter position which specifies the iterator that is the reference to the position of the element to be erased. Return Value: The function does not returns anything. Program below illustrate the above syntax:
    Output:
    The multimap before using erase() is : 
    KEY ELEMENT
    1 40
    2 30
    2 20
    3 60
    5 50
    
    The multimap after applying erase() is : 
    KEY ELEMENT
    3 60
    2 20
    1 40
    
  3. Syntax for erasing a given range:
    multimap_name.erase(iterator position1, iterator position2)
    
    Parameters: The function accepts two mandatory parameters which are described below:
    • position1 - specifies the iterator that is the reference to the element from which removal is to be done.
    • position2 - specifies the iterator that is the reference to the element upto which removal is to be done.
    Return Value: The function does not returns anything. It removes all the elements in the given range of iterators. Program below illustrate the above syntax:
Output:
The multimap before using erase() is : 
KEY ELEMENT
1 40
2 30
2 20
3 60
5 50

The multimap after applying erase() is : 
KEY ELEMENT
5 50
1 40
Comment
Article Tags: