Home » C++ STL

Delete elements from a Map | C++ STL

C++ STL | std::map::erase() and std::map::clear() functions with Example: Here, we are going to learn how to delete elements from a Map in C++ STL?
Submitted by Vivek Kothari, on December 15, 2018

We can use std::map::erase() function to erase single element or range of elements in a map. If we want to delete all the elements in a map we can use std::map::clear() function. Let discuss these functions in detail.

1. std::map::erase() function

It removes the element from a map and reduces the size of a map by 1.

Syntax:

    //Erase by using iterator: 
    MapName.erase (it);
    //Where ‘it’ is iterator of type map.

    //Erasing by key :
    MapName.erase (key);

    //Erasing by range :
    MapName.erase (first,last);
    //Where the range includes all the elements of the map 
    //between first and lasti.e[first,last).

Program:

#include <bits/stdc++.h>
using namespace std;

int main ()
{

	map<char,int> MyMap;

	// insert some elements in map
	MyMap['a']=1;
	MyMap['b']=2;
	MyMap['c']=3;
	MyMap['d']=4;
	MyMap['e']=5;
	MyMap['f']=6;
	MyMap['g']=7;

	map<char,int>::iterator it;

	cout<<"Elements in map : "<<endl;
	for (it = MyMap.begin(); it != MyMap.end(); it++) 
	{ 
		cout  <<"key = "<< it->first <<" value = "<< it->second <<endl; 
	}
	cout<<endl;  
	
	//delete using key
	MyMap.erase ('f');

	// erasing by iterator
	it=MyMap.find('e');
	MyMap.erase (it); 

	cout<<"Elements in map after deleting f and e: "<<endl;
	for (it = MyMap.begin(); it != MyMap.end(); it++) 
	{ 
		cout  <<"key = "<< it->first <<" value = "<< it->second <<endl; 
	}
	cout<<endl;  
	
	// erasing by range
	// note that last is not inclusive
	//delete elements from a to c 
	it = MyMap.find('d');             
	MyMap.erase ( MyMap.begin() , it );   

	cout<<"Elements in map after deleting a to c : "<<endl;
	for (it = MyMap.begin(); it != MyMap.end(); it++) 
	{ 
		cout  <<"key = "<< it->first <<" value = "<< it->second <<endl; 
	}

	return 0;
}

Output

Elements in map :
key = a value = 1
key = b value = 2
key = c value = 3
key = d value = 4
key = e value = 5
key = f value = 6
key = g value = 7

Elements in map after deleting f and e:
key = a value = 1
key = b value = 2
key = c value = 3
key = d value = 4
key = g value = 7

Elements in map after deleting a to c :
key = d value = 4
key = g value = 7

2. std::map::clear() function

This function removes all elements from a map and reduces map size to 0.

Syntax:

    MapName.clear();
    //There is no parameter to pass.

Program:

#include <bits/stdc++.h>
using namespace std;

int main ()
{
	map<char,int> MyMap;

	// insert some elements in map
	MyMap['a']=1;
	MyMap['b']=2;
	MyMap['c']=3;
	MyMap['d']=4;
	MyMap['e']=5;
	MyMap['f']=6;
	MyMap['g']=7;

	map<char,int>::iterator it;

	cout<<"Elements in map : "<<endl;
	for (it = MyMap.begin(); it != MyMap.end(); it++) 
	{ 
		cout  <<"key = "<< it->first <<" value = "<< it->second <<endl; 
	}
	cout<<endl;

	//erasing all elements    
	MyMap.clear();

	cout<<"After applying MyMap.clear() function : "<<endl;
	if(MyMap.size() == 0 )
		cout<<"map is empty !!!";

	return 0;
}

Output

Elements in map :
key = a value = 1
key = b value = 2
key = c value = 3
key = d value = 4
key = e value = 5
key = f value = 6
key = g value = 7

After applying MyMap.clear() function :
map is empty !!!





Comments and Discussions

Ad: Are you a blogger? Join our Blogging forum.



Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing » Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.