c ++ stl
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.
我们可以使用std :: map :: erase()函数来删除地图中的单个元素或元素范围。 如果要删除地图中的所有元素,可以使用std :: map :: clear()函数。 让我们详细讨论这些功能。
1. std :: map :: erase()函数 (1. std::map::erase() function )
It removes the element from a map and reduces the size of a map by 1.
它将元素从地图中删除,并将地图大小减小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()函数 (2. std::map::clear() function )
This function removes all elements from a map and reduces map size to 0.
此功能从地图上删除所有元素,并将地图大小减小为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 !!!
翻译自: https://www.includehelp.com/stl/delete-elements-from-a-map.aspx
c ++ stl