📜  C++ STL中的set :: erase

📅  最后修改于: 2021-05-30 13:14:01             🧑  作者: Mango

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

设置:: 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 
#include 
  
using namespace std;
  
int main()
{
    // set declaration
    set myset{ 1, 2, 3, 4, 5 };
    set::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 
#include 
  
using namespace std;
  
int main()
{
    // set declaration
    set myset{ 'A', 'C', 'E', 'G' };
    set::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 
#include 
  
using namespace std;
  
int main()
{
    // set declaration
    set myset{ 1, 2, 3, 4, 5 };
    set::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 
#include 
  
using namespace std;
  
int main()
{
    // set declaration
    set myset{ 'A', 'B', 'C', 'D' };
    set::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. setname .erase( position )–摊销常数
2.的setName .erase(startingposition,endingposition) – 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 
#include 
  
using namespace std;
  
int main()
{
    // set declaration
    set 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++基础课程为基础,以先进的C++和C++ STL课程基础加上STL。要完成从学习语言到DS Algo等的更多准备工作,请参阅“完整面试准备课程”