无涯教程-C++ Map - erase函数

59 阅读3分钟

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 << 
;
  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 << 
;
  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 << 
;

 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 << 
;
  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()<<
;
   for (it=mymap.begin(); it!=mymap.end(); ++it)
   cout << it->first << " => " << it->second << 
;

   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 << 
;
  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,

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

参考链接

www.learnfk.com/c++/cpp-map…