C++ map erase() 函数
C++ map erase() 函数用于从 map 容器中删除与给定键值关联的单个元素或一系列元素。因此,容器的大小将减少被删除的元素数量。
语法
void erase (iterator position); //until C++ 11
size_type erase (const key_type& k); //until C++ 11
void erase (iterator first, iterator last); //until C++ 11
iterator erase (const_iterator position); //since C++ 11
size_type erase (const key_type& k); //since C++ 11
iterator erase (const_iterator first, const_iterator last); //since C++ 11
参数
position :指向要从地图中删除的单个元素的迭代器。
k :要从地图中删除的元素的键。
first :要清除的范围的起始位置。
last :要清除的范围的结束位置。
返回值
它返回一个指向被删除元素的下一个元素的迭代器,或者返回删除元素的数量。
示例1
让我们看一个通过迭代器删除元素的简单示例。
#include <iostream>
#include <map>
using namespace std;
int main ()
{
map<char,int> mymap;
map<char,int>::iterator it;
mymap['a']=10;
mymap['b']=20;
mymap['c']=30;
mymap['d']=40;
cout<<"Before erasing the element: \n";
for (it=mymap.begin(); it!=mymap.end(); ++it)
std::cout << it->first << " => " << it->second << '\n';
it=mymap.find('b');
mymap.erase (it); // erasing by iterator
cout<<"\nAfter erasing the element: \n";
for (it=mymap.begin(); it!=mymap.end(); ++it)
std::cout << it->first << " => " << it->second << '\n';
return 0;
}
输出:
Before erasing the element:
a => 10
b => 20
c => 30
d => 40
After erasing the element:
a => 10
c => 30
d => 40
在上面的示例中,元素由迭代器it擦除。
示例2
让我们来看一个简单的示例,通过给定的键值从映射中删除元素。
#include <iostream>
#include <map>
using namespace std;
int main ()
{
map<char,int> mymap;
map<char,int>::iterator it;
mymap['a']=10;
mymap['b']=20;
mymap['c']=30;
mymap['d']=40;
cout<<"Before erasing the element: \n";
for (it=mymap.begin(); it!=mymap.end(); ++it)
std::cout << it->first << " => " << it->second << '\n';
mymap.erase ('c'); // erasing by key
cout<<"\nAfter erasing the element: \n";
for (it=mymap.begin(); it!=mymap.end(); ++it)
std::cout << it->first << " => " << it->second << '\n';
return 0;
}
输出:
Before erasing the element:
a => 10
b => 20
c => 30
d => 40
After erasing the element:
a => 10
b => 20
d => 40
在上面的示例中,erase(key)函数使用键值’c’和对应的值从map中删除。
示例3
让我们看一个简单的示例,通过给定的范围删除元素。
#include <iostream>
#include <map>
using namespace std;
int main ()
{
map<char,int> mymap;
map<char,int>::iterator it;
mymap['a']=10;
mymap['b']=20;
mymap['c']=30;
mymap['d']=40;
cout<<"Before erasing the element are: \n";
cout<<"Size is: "<<mymap.size()<<'\n';
for (it=mymap.begin(); it!=mymap.end(); ++it)
cout << it->first << " => " << it->second << '\n';
mymap.erase ( mymap.begin () , mymap.end () ); // erasing by range
cout<<"\nAfter erasing the element are: \n";
cout<<"Size is: "<<mymap.size();
for (it=mymap.begin(); it!=mymap.end(); ++it)
cout << it->first << " => " << it->second << '\n';
return 0;
}
输出:
Before erasing the element are:
Size is: 4
a => 10
b => 20
c => 30
d => 40
After erasing the element are:
Size is: 0
在上面的示例中,使用erase(first, last)函数来删除给定范围内的元素,即从开始到结束。
示例4
让我们看一个简单的示例来从地图中删除所有奇数。
#include <map>
#include <iostream>
using namespace std;
int main()
{
map<int, string> m = {{1, "one"},
{2, "two"},
{3, "three"},
{4, "four"},
{5, "five"},
{6, "six"}};
// erase all odd numbers from m
cout<<"After erasing odd numbers,elements are:\n ";
for(auto it = m.begin(); it != m.end(); )
if(it->first % 2 == 1)
it = m.erase(it);
else
++it;
for(auto& p : m)
cout << p.second << ", ";
}
输出:
After erasing odd numbers,elements are:
two, four, six,
在上面的示例中,所有的奇数都已经被删除,只显示偶数。