C++ STL中的set::erase用法介绍

2021年4月27日17:13:10 发表评论 1,502 次浏览

set是一种关联容器, 其中每个元素都必须是唯一的, 因为元素的值可以标识它。尽管可以删除并添加该元素的修改后的值, 但是一旦将元素的值添加到集合中就无法对其进行修改。

set::erase()

delete()函数用于从指定位置或范围中删除容器中的元素。

句法 :

1. setname.erase(position)
2. setname.erase(startingposition, endingposition)
Parameters :
Position of the element to be removed in 
the form of iterator or the range specified
using start and end iterator.
Result :
Elements are removed from the specified
position of the container.

例子:

Input  : myset{1, 2, 3, 4, 5}, iterator= 2
         myset.erase(iterator);
Output : 1, 2, 4, 5

Input  : myset{1, 2, 3, 4, 5, 6, 7, 8}, iterator1= 3, iterator2= 6
         myset.erase(iterator1, iterator2);
Output : 1, 2, 3, 8

错误和异常

1.如果位置有效, 则没有异常抛出保证。

2.否则显示未定义的行为。

从特定位置移除元件

//INTEGER SET EXAMPLE
//CPP program to illustrate
//Implementation of erase() function
#include <iostream>
#include <set>
  
using namespace std;
  
int main()
{
     //set declaration
     set<int> myset{ 1, 2, 3, 4, 5 };
     set<int>::iterator it1, it2;
  
     //defining it1 pointing to the first
     //element and it2 to the last element
     it1 = myset.begin();
     it2 = myset.end();
  
     //decrementing the it2 two times
     it2--;
     it2--;
  
     //erasing elements within the range
     //of it1 and it2
     myset.erase(it1, it2);
  
     //Printing the set
     for ( auto it = myset.begin();
         it != myset.end(); ++it)
         cout <<' ' <<*it;
     return 0;
}

输出如下:

4 5
//CHARACTER SET EXAMPLE
//CPP program to illustrate
//Implementation of erase() function
#include <iostream>
#include <set>
  
using namespace std;
  
int main()
{
     //set declaration
     set<char> myset{ 'A' , 'C' , 'E' , 'G' };
     set<char>::iterator it1, it2;
  
     //defining it1 pointing to the first
     //element and it2 to the last element
     it1 = myset.begin();
     it2 = myset.end();
  
     //decrementing the it2 two times
     it2--;
     it2--;
  
     //erasing elements within the
     //range of it1 and it2
     myset.erase(it1, it2);
  
     //Printing the set
     for ( auto it = myset.begin();
         it != myset.end(); ++it)
         cout <<' ' <<*it;
     return 0;
}

输出如下:

E G

删除范围内的元素

//INTEGER SET EXAMPLE
//CPP program to illustrate
//Implementation of erase() function
#include <iostream>
#include <set>
  
using namespace std;
  
int main()
{
     //set declaration
     set<int> myset{ 1, 2, 3, 4, 5 };
     set<int>::iterator it;
  
     //defining iterator pointing
     //to the first element
     it = myset.begin();
  
     //erasing the first element
     myset.erase(it);
  
     //Printing the set
     for ( auto it = myset.begin();
         it != myset.end(); ++it)
         cout <<' ' <<*it;
     return 0;
}

输出如下:

2 3 4 5
//CHARACTER SET EXAMPLE
//CPP program to illustrate
//Implementation of erase() function
#include <iostream>
#include <set>
  
using namespace std;
  
int main()
{
     //set declaration
     set<char> myset{ 'A' , 'B' , 'C' , 'D' };
     set<char>::iterator it;
  
     //defining iterator pointing
     //to the first element
     it = myset.begin();
  
     //erasing the first element
     myset.erase(it);
  
     //Printing the set
     for ( auto it = myset.begin();
         it != myset.end(); ++it)
         cout <<' ' <<*it;
     return 0;
}

输出如下:

B C D

时间复杂度:

1. erase(position)——平摊常量

2. setname.erase(起始位置,结束位置)- O(n), n是起始位置和结束位置之间的元素数。

应用

给定一组整数, 请从该组中删除所有偶数元素, 然后打印该组。

Input  :1, 2, 3, 4, 5, 6, 7, 8, 9
Output :1 3 5 7 9
Explanation - 2, 4, 6 and 8 which are even are erased from the set

算法

1.循环运行直至达到集合的大小。

2.检查每个位置的元素是否可被2整除, 如果是, 则删除该元素, 并将返回迭代器分配给当前迭代器, 如果否, 则将迭代器递增。

3.打印最后一组。

注意:erase返回下一个元素的迭代器

//CPP program to illustrate
//Application of erase() function
#include <iostream>
#include <set>
  
using namespace std;
  
int main()
{
     //set declaration
     set<int> myset{ 1, 2, 3, 4, 5, 6, 7, 8, 9 };
  
     //checking for even elements and removing them
     for ( auto i = myset.begin(); i != myset.end(); ) {
         if (*i % 2 == 0) 
             i=myset.erase(i);
         else
             i++; 
          
     }
  
     //Printing the set
     for ( auto it = myset.begin(); it != myset.end(); ++it)
         cout <<' ' <<*it;
     return 0;
}

输出:

1 3 5 7 9

被认为是行业中最受欢迎的技能之一, 我们拥有自己的编码基础C++++ STL通过激烈的问题解决过程来训练和掌握这些概念。


木子山

发表评论

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: