multimap::erase() in C++ STL

C++Server Side ProgrammingProgramming

In this article we will be discussing the working, syntax, and examples of multimap::erase() function in C++ STL.

What is Multimap in C++ STL?

Multimaps are the associative containers, which are similar to map containers. It also facilitates storing the elements formed by a combination of key-value and mapped value in a specific order. In a multimap container, there can be multiple elements associated with the same key. The data is internally always sorted with the help of its associated keys.

What is multimap::erase()?

multimap::erase() function is an inbuilt function in C++ STL, which is defined in <map> header file. erase() is used to remove or erase elements from a multimap container.

This function can remove or erase the elements by its key, position or the given range. When we run this function the size of the multimap container is reduced by the number of elements being removed.

Syntax

multimap_name.erase(key_type key);
multimap_name.erase(const_iterator it);
multimap_name.erase(const_ iterator start, const_itertaor end);

Parameters

The function accepts the following parameter(s) −

  • key − This is the position of the key whose elements we wish to remove.

  • it − The iterator position from where we wish to remove the elements.

  • start, end − This defines the range from which position till what position the set of elements should be removed.

Return value

This function returns the number of elements being removed from the associated container.

Input 

std::multimap<int> mymap;
mymap.insert({‘a’, 10});
mymap.insert({‘b’, 20});
mymap.insert({‘c’, 30});
mymap.erase(‘b’);

Output 

a:10 c:30

Example

//erase a given key

 Live Demo

#include<iostream>
#include<map>
using namespace std;
int main(){
   multimap<int,char > mul_1;
   //declaring iterator to traverse the elements
   multimap<int,char>:: iterator i;
   //inserting elements to multimap1
   mul_1.insert(make_pair(0,'a'));
   mul_1.insert(make_pair(1,'b'));
   mul_1.insert(make_pair(2,'c'));
   mul_1.insert(make_pair(3,'d'));
   mul_1.insert(make_pair(4,'e'));
   mul_1.insert(make_pair(5,'f'));
   mul_1.insert(make_pair(6,'g'));
   //calling erase() to delete the element
   mul_1.erase(1);
   mul_1.erase(4);
   mul_1.erase(6);
   mul_1.erase(5);
   //elements of multimap1
   cout<<"Elements in multimap1 are: "<<"\n";
   for( i = mul_1.begin(); i!= mul_1.end(); i++){
      cout<<(*i).first<<" "<< (*i).second << "\n";
   }
}

Output

If we run the above code it will generate the following output −

Elements in multimap1 are:
0 a
2 c
3 d

Example

//erasing elements within a given range

 Live Demo

#include<iostream>
#include<map>
using namespace std;
int main(){
   multimap<int,char > mul_1;
   //declaring iterator to traverse the elements
   multimap<int,char>:: iterator i;
   //inserting elements to multimap1
   mul_1.insert(make_pair(0,'a'));
   mul_1.insert(make_pair(1,'b'));
   mul_1.insert(make_pair(2,'c'));
   mul_1.insert(make_pair(3,'d'));
   mul_1.insert(make_pair(4,'e'));
   mul_1.insert(make_pair(5,'f'));
   mul_1.insert(make_pair(6,'g'));
   //elements in multimap before erasing
   cout<<"Elements in multimap1 are: "<<"\n";
   for( i = mul_1.begin(); i!= mul_1.end(); i++){
      cout<<(*i).first<<" "<< (*i).second << "\n";
   }
   //calling erase() to delete the element
   auto start = mul_1.find(3);
   auto end = mul_1.find(6);
   mul_1.erase(start, end);
   //elements of multimap1 after erasing
   cout<<"Elements in multimap1 are: "<<"\n";
   for( i = mul_1.begin(); i!= mul_1.end(); i++){
      cout<<(*i).first<<" "<< (*i).second << "\n";
   }
}

Output

If we run the above code it will generate the following output −

Elements in multimap1 are:
0 a
1 b
2 c
3 d
4 e
5 f
6 g
Elements in multimap1 are:
0 a
1 b
2 c
6 g

Example

//erasing element at the given position

 Live Demo

#include<iostream>
#include<map>
using namespace std;
int main(){
   multimap<int,char > mul_1;
   //declaring iterator to traverse the elements
   multimap<int,char>:: iterator i;
   //inserting elements to multimap1
   mul_1.insert(make_pair(0,'a'));
   mul_1.insert(make_pair(1,'b'));
   mul_1.insert(make_pair(2,'c'));
   mul_1.insert(make_pair(3,'d'));
   mul_1.insert(make_pair(4,'e'));
   mul_1.insert(make_pair(5,'f'));
   mul_1.insert(make_pair(6,'g'));
   //elements in multimap before erasing
   cout<<"Elements in multimap1 are: "<<"\n";
   for( i = mul_1.begin(); i!= mul_1.end(); i++){
      cout<<(*i).first<<" "<< (*i).second << "\n";
   }
   //calling erase() to delete the element
   auto first = mul_1.find(1);
   mul_1.erase(first);
   auto second = mul_1.find(6);
   mul_1.erase(second);
   auto third = mul_1.find(2);
   mul_1.erase(third);
   //elements of multimap1 after erasing
   cout<<"Elements in multimap1 are: "<<"\n";
   for( i = mul_1.begin(); i!= mul_1.end(); i++){
      cout<<(*i).first<<" "<< (*i).second << "\n";
   }
}

Output

If we run the above code it will generate the following output −

Elements in multimap1 are:
0 a
1 b
2 c
3 d
4 e
5 f
6 g
Elements in multimap1 are:
0 a
3 d
4 e
5 f
raja
Published on 22-Apr-2020 11:26:08
Advertisements