C++ Map 中的 erase函数

首页 / C++入门教程 / C++ Map 中的 erase函数

C++映射 erase()函数用于从映射集合中删除与给定键值关联的单个元素或一系列元素。因此,将通过删除元素的数量来减小尺寸。

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

erase - 参数

position   -  指向要从映射中删除的单个元素的迭代器。

k                -  要从映射中删除的元素的键。

first          -  要擦除的参数的开始。

last           -  要擦除的参数的结尾。

erase - 返回值

它返回一个指向已删除元素的下一个元素的迭代器,或者返回已删除元素的数量。

erase - 例子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

在上面的示例中,元素被迭代器擦除。

erase - 例子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'及其映射中的值。

erase - 例子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)函数来擦除具有给定参数(即开始到结束)的元素。

erase - 例子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,

在上面的示例中,所有奇数均已删除,并显示偶数。

祝学习愉快!(内容编辑有误?请选中要编辑内容 -> 右键 -> 修改 -> 提交!)

技术教程推荐

技术与商业案例解读 -〔徐飞〕

Linux性能优化实战 -〔倪朋飞〕

TensorFlow快速入门与实战 -〔彭靖田〕

性能测试实战30讲 -〔高楼〕

图解 Google V8 -〔李兵〕

打造爆款短视频 -〔周维〕

操作系统实战45讲 -〔彭东〕

手把手带你搭建秒杀系统 -〔佘志东〕

手把手带你搭建推荐系统 -〔黄鸿波〕

好记忆不如烂笔头。留下您的足迹吧 :)