Open In App

unordered_multimap erase in C++ STL

Last Updated : 14 Dec, 2018
Improve
Improve
Like Article
Like
Save
Share
Report

unordered_multimap::erase() is a built in function in C++ STL which removes the element from given range, by position and by key. There are three variant of this function in C++ STL.
There are following type of erase() functions in C++ for unordered_multimap.

  1. By position : It removes element from unordered_multimap by given position and return an iterator pointing to the position immediately following the last of the elements erased.
  2. By key : It removes elements by the key. It returns the number of elements which has been erased.
  3. By range : It takes iterator first and last and removes all elements between them including first but excluding last.It return an iterator pointing to the position immediately following the last of the elements erased.

Syntax:

  1. iterator erase ( iterator position )
  2. size erase ( key_type& k )
  3. iterator erase ( iterator first, iterator last );

Below programs explains about above functions.
Example 1




// C++ program to illustrate the
// unordered_multimap::erase() function
#include <bits/stdc++.h>
using namespace std;
  
int main()
{
  
    // declaration of unordered_multimap
    unordered_multimap<char, int> sample;
  
    // inserts element
    sample.insert({ 'a', 2 });
    sample.insert({ 'b', 4 });
    sample.insert({ 'c', 8 });
    sample.insert({ 'd', 10 });
    sample.insert({ 'c', 4 });
    sample.insert({ 'e', 4 });
    sample.insert({ 'f', 4 });
    cout << " Elements of multimap are : \n";
    for (auto& x : sample)
        cout << x.first << " : " << x.second << endl;
  
    // delete element by position
    sample.erase(sample.begin());
  
    // print after delete by position
    cout << " Elements of multimap after deleting by position are : \n";
    for (auto& x : sample)
        cout << x.first << " : " << x.second << endl;
  
    // erase by Element
    sample.erase('c');
  
    // print after delete by element
    cout << " Elements of multimap after deleting by element name : \n";
    for (auto& x : sample)
        cout << x.first << " : " << x.second << endl;
  
    // erase by range
    sample.erase(sample.find('e'), sample.end());
  
    // print after delete by range
    cout << " Elements of multimap after deleting by range are : \n";
    for (auto& x : sample)
        cout << x.first << " : " << x.second << endl;
    return 0;
}


Output:

Elements of multimap are : 
f : 4
b : 4
a : 2
c : 4
c : 8
d : 10
e : 4
 Elements of multimap after deleting by position are : 
b : 4
a : 2
c : 4
c : 8
d : 10
e : 4
 Elements of multimap after deleting by element name : 
b : 4
a : 2
d : 10
e : 4
 Elements of multimap after deleting by range are : 
b : 4
a : 2
d : 10

Example 2




// C++ program to illustrate the
// unordered_multimap::erase() function
#include <bits/stdc++.h>
using namespace std;
  
int main()
{
  
    // declaration of unordered_multimap
    unordered_multimap<int, int> sample;
  
    // inserts element
    sample.insert({ 1, 2 });
    sample.insert({ 2, 4 });
    sample.insert({ 3, 8 });
    sample.insert({ 4, 10 });
    sample.insert({ 3, 4 });
    sample.insert({ 5, 4 });
    sample.insert({ 6, 4 });
    cout << " Elements of multimap are : \n";
    for (auto& x : sample)
        cout << x.first << " : " << x.second << endl;
  
    // delete element by position
    sample.erase(sample.begin());
  
    // print after delete by position
    cout << " Elements of multimap after deleting by position are : \n";
    for (auto& x : sample)
        cout << x.first << " : " << x.second << endl;
  
    // erase by Element
    sample.erase(3);
  
    // print after delete by element
    cout << " Elements of multimap after deleting by element name : \n";
    for (auto& x : sample)
        cout << x.first << " : " << x.second << endl;
  
    // erase by range
    sample.erase(sample.find(5), sample.end());
  
    // print after delete by range
    cout << " Elements of multimap after deleting by range are : \n";
    for (auto& x : sample)
        cout << x.first << " : " << x.second << endl;
    return 0;
}


Output:

Elements of multimap are : 
6 : 4
2 : 4
1 : 2
3 : 4
3 : 8
4 : 10
5 : 4
 Elements of multimap after deleting by position are : 
2 : 4
1 : 2
3 : 4
3 : 8
4 : 10
5 : 4
 Elements of multimap after deleting by element name : 
2 : 4
1 : 2
4 : 10
5 : 4
 Elements of multimap after deleting by range are : 
2 : 4
1 : 2
4 : 10


Previous Article
Next Article

Similar Reads

How to erase an element from a vector using erase() and reverse_iterator?
Given a vector, the task is to erase an element from this vector using erase() and reverse_iterator. Example: Input: vector = {1, 4, 7, 10, 13, 16, 19}, element = 16 Output: 1 4 7 10 13 19 Input: vector = {99, 89, 79, 69, 59}, element = 89 Output: 99 79 69 59 Approach: Get the vector and the element to be deleted Initialize a reverse iterator on th
3 min read
unordered_multimap begin() and end() function in C++ STL
The unordered_multimap::begin() is a built-in function in C++ STL that returns an iterator pointing to the first element in the container or to the first element in one of its buckets. Syntax: unordered_multimap_name.begin(n) Parameters: The function accepts one parameter. If a parameter is passed, it returns an iterator pointing to the first eleme
5 min read
unordered_multimap key_eq() function in C++ STL
unordered_multimap::key_eq() is a built-in function in C++ STL which returns a boolean value according to the comparison. It depends on the key equivalence comparison predicate used by the unordered_multimap container. The key equivalence comparison is a predicate which takes two arguments and returns a boolean value indicating whether they are to
2 min read
unordered_multimap hash_function() in C++ STL
The unordered_multimap::hash_function() is a built-in function in C++ STL which is used to get the hash function. This hash function is a unary function which takes a single argument only and returns a unique value of type size_t based on it. Syntax: unordered_multimap_name.hash_function() Parameter: The function does not accept any parameter. Retu
2 min read
unordered_multimap max_bucket_count() function in C++ STL
The unordered_multimap::max_bucket_count() is a built-in function in C++ STL which returns the maximum number of buckets that the unordered multimap container can have. This is the maximum it can have, it cannot exceed despite the collisions due to certain limitations on it. Syntax: unordered_multimap_name.max_bucket_count() Parameter: The function
2 min read
unordered_multimap find() function in C++ STL
The unordered_multimap::find() is a built-in function in C++ STL which returns an iterator which points to one of the elements which has the key k. If the container does not contain any element with key k, then it returns an iterator which points to the position which is past the last element in the container. Syntax: unordered_multimap_name.find(k
3 min read
unordered_multimap load_factor() function in C++ STL
The unordered_multimap::load_factor() is a built-in function in C++ STL which returns returns the current load factor in the unordered_multimap container. The load factor is the ratio between the number of elements in the container (its size) and the number of buckets (bucket_count): load_factor = size / bucket_count The load factor influences the
3 min read
unordered_multimap equal_range() function in C++ STL
unordered_multimap::equal_range() is a built-in function in C++ STL which returns the range in which all the element's key is equal to a key. It returns a pair of iterators where the first is an iterator pointing to the lower bound of the range and second is an iterator pointing to the upper bound of the range. If there is no element equal to a giv
3 min read
unordered_multimap empty() function in C++ STL
The unordered_multimap::empty() is a built-in function in C++ STL which returns a boolean value. It returns true if the unordered_multimap container is empty. Otherwise, it returns false. Syntax: unordered_multimap_name.empty() Parameters: The function does not accept any parameter. Return Value: It returns a boolean value which denotes whether a u
2 min read
unordered_multimap emplace_hint() function in C++ STL
The unordered_multimap::emplace_hint() is a built-in function in C++ STL which inserts a new {key:element} in the unordered_multimap container. It starts searching from the position provided in the parameter for the insertion point of the element. The position only acts as a hint, it does not decide the position at which the insertion is to be done
2 min read
Article Tags :
Practice Tags :