C++ Map erase()
C++ Map erase()函数
C++映射 erase()函数用于删除与给定键值关联的单个元素或地图容器中元素的范围。因此,将通过删除元素的数量来减小大小。
语法
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
参数
pos: 指向要从地图中删除的单个元素的迭代器。
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
在上面的示例中,元素被迭代器擦除。
示例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
在上面的示例中,delete(key)函数使用键值'c'及其来自映射的值。
示例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
在上面的示例中,使用擦除(第一个,最后一个)函数擦除具有给定范围(即开始到结束)的元素。
示例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,
在上面的示例中,所有奇数均已删除,并显示偶数。