C++ Set 中的 Erase函数

首页 / C++入门教程 / C++ Set 中的 Erase函数

C++ set erase()函数用于从set集合中删除与给定键关联的单个元素或元素参数([first,last))。因此,将通过删除元素的数量来减小尺寸。

Erase - 语法

void erase (iterator position);                       	  //until C++ 11

size_type erase (const value_type& val);    		  //until C++ 11

void erase (iterator first, iterator last);  		  //until C++ 11

iterator  erase (const_iterator position);		  //since C++ 11

size_type erase (const value_type& val);		  //since C++ 11	

iterator  erase (const_iterator first, const_iterator last); //since C++ 11

Erase - 参数

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

val           -  要从集合中删除的值。

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

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

Erase - 返回值

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

Erase - 例子1

让我们看一个简单的示例,该示例通过迭代器擦除元素。

#include <iostream>
#include <set>

using namespace std;

int main ()
{
  set<int> myset;
  set<int>::iterator it;

  myset = {10,20,30};
  
  cout<<"Before erasing the element: \n";
   for (it=myset.begin(); it!=myset.end(); ++it)
    cout << *it << '\n';

  it=myset.find('b');
  myset.erase (*it);                  //erasing by iterator

  cout<<"\nAfter erasing the element: \n";
  for (it=myset.begin(); it!=myset.end(); ++it)
    cout << *it << '\n';
    
  return 0;
}

输出:

Before erasing the element: 
10
20
30

After erasing the element: 
10
20
30

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

Erase - 例子2

让我们看一个简单的示例,用给定的键值擦除集合中的元素:

#include <iostream>
#include <set>

using namespace std;

int main ()
{
  set<int> myset;
  set<int>::iterator it;

  myset = {10, 20, 30, 40};
  
  cout<<"Before erasing the element: \n";
   for (it=myset.begin(); it!=myset.end(); ++it)
    cout << *it<< '\n';

   myset.erase (30);                 //erasing by value

  cout<<"\nAfter erasing the element: \n";
  for (it=myset.begin(); it!=myset.end(); ++it)
    cout << *it<< '\n';
  return 0;
}

输出:

Before erasing the element: 
10
20
30
40

After erasing the element: 
10
20
40

在上面的示例中,delete(value)函数使用集合中的值30。

Erase - 例子3

让我们看一个简单的示例,以给定参数擦除元素:

#include <iostream>
#include <set>

using namespace std;

int main ()
{
  set<int> myset;
  set<int>::iterator it;

  myset = {10, 20, 30};
  
  cout<<"Before erasing the element are: \n";
   cout<<"Size is: "<<myset.size()<<'\n';
   for (it=myset.begin(); it!=myset.end(); ++it)
   cout << *it << '\n';

   myset.erase ( myset.begin () ,  myset.end () );  //erasing by range

  cout<<"\nAfter erasing the element are: \n";
  cout<<"Size is: "<<myset.size();
  for (it=myset.begin(); it!=myset.end(); ++it)
  cout << *it << '\n';
  return 0;
}

输出:

Before erasing the element are: 
Size is: 3
10
20
30

After erasing the element are: 
Size is: 0

在上述示例中,使用了Erase(first,last)函数来擦除具有给定参数(即开始到结束)的元素。

Erase - 例子4

让我们看一个简单的示例,以删除集合中的所有奇数:

#include <set>
#include <iostream>

using namespace std;

int main()
{
    set<int> m = {1,2,3,4,5,6,7,8,9,10,11,12,13,14,15};
                          
   //erase all odd numbers from m
    cout<<"After erasing odd numbers,elements are:\n ";
    for(auto it = m.begin(); it != m.end(); )
        if(*it % 2 == 1)
            it = m.erase(it);
        else
            ++it;
    for(auto& p : m)
        cout << p << ", ";
}

输出:

After erasing odd numbers, elements are:
 2, 4, 6, 8, 10, 12, 14,

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

Erase - 例子5

让我们看另一个例子:

#include <set>  
#include <string>  
#include <iostream>  
#include <iterator>//next() and prev() helper functions  
  
using namespace std;  
  
using myset = set<string>;  
  
void printset(const myset& s) {  
    for (const auto& iter : s) {  
        cout << " [" << iter << "]";  
    }  
    cout << endl << "size() == " << s.size() << endl << endl;  
}  
  
int main()  
{  
    myset s1;  
  
   //Fill in some data to test with, one at a time  
    s1.insert("Bob");  
    s1.insert("Robert");  
    s1.insert("Bert");  
    s1.insert("Rob");  
    s1.insert("Bobby");  
  
    cout << "Starting data of set s1 is:" << endl;  
    printset(s1);  
   //The 1st member function removes an element at a given position  
    s1.erase(next(s1.begin()));  
    cout << "After the 2nd element is deleted, the set s1 is:" << endl;  
    printset(s1);  
  
   //Fill in some data to test with, one at a time, using an intializer list  
    myset s2{ "meow", "hiss", "purr", "growl", "yowl" };  
  
    cout << "Starting data of set s2 is:" << endl;  
    printset(s2);  
   //The 2nd member function removes elements  
   //in the range [First, Last)  
    s2.erase(next(s2.begin()), prev(s2.end()));  
    cout << "After the middle elements are deleted, the set s2 is:" << endl;  
    printset(s2);  
  
    myset s3;  
  
   //Fill in some data to test with, one at a time, using emplace  
    s3.emplace("C");  
    s3.emplace("C#");  
    s3.emplace("D");  
    s3.emplace("D#");  
    s3.emplace("E");  
    s3.emplace("E#");  
    s3.emplace("F");  
    s3.emplace("F#");  
    s3.emplace("G");  
    s3.emplace("G#");  
    s3.emplace("A");  
    s3.emplace("A#");  
    s3.emplace("B");  
  
    cout << "Starting data of set s3 is:" << endl;  
    printset(s3);  
   //The 3rd member function removes elements with a given Key  
    myset::size_type count = s3.erase("E#");  
   //The 3rd member function also returns the number of elements removed  
    cout << "The number of elements removed from s3 is: " << count << "." << endl;  
    cout << "After the element with a key of \"E#\" is deleted, the set s3 is:" << endl;  
    printset(s3);  
}  

输出:

Starting data of set s1 is:
 [Bert] [Bob] [Bobby] [Rob] [Robert]
size() == 5

After the 2nd element is deleted, the set s1 is:
 [Bert] [Bobby] [Rob] [Robert]
size() == 4

Starting data of set s2 is:
 [growl] [hiss] [meow] [purr] [yowl]
size() == 5

After the middle elements are deleted, the set s2 is:
 [growl] [yowl]
size() == 2

Starting data of set s3 is:
 [A] [A#] [B] [C] [C#] [D] [D#] [E] [E#] [F] [F#] [G] [G#]
size() == 13

The number of elements removed from s3 is: 1.
After the element with a key of "E#" is deleted, the set s3 is:
 [A] [A#] [B] [C] [C#] [D] [D#] [E] [F] [F#] [G] [G#]
size() == 12

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

技术教程推荐

从0开始学大数据 -〔李智慧〕

许式伟的架构课 -〔许式伟〕

Kafka核心技术与实战 -〔胡夕〕

深度学习推荐系统实战 -〔王喆〕

编程高手必学的内存知识 -〔海纳〕

B端体验设计入门课 -〔林远宏(汤圆)〕

PPT设计进阶 · 从基础操作到高级创意 -〔李金宝(Bobbie)〕

手把手带你写一个 MiniTomcat -〔郭屹〕

程序员职业规划手册 -〔雪梅〕

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