集是一种关联容器,其中每个元素都必须是唯一的,因为元素的值可以标识它。尽管可以删除并添加该元素的修改后的值,但是一旦将元素的值添加到集合中就无法对其进行修改。
set::erase()
erase() 函数用于从容器中的指定位置或范围移除元素。
用法:
1. setname.erase(position) 2. setname.erase(startingposition, endingposition) 参数: 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. setname.erase(position) - 摊销常数2。 setname.erase(startingposition, endposition) - 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
相关用法
注:本文由纯净天空筛选整理自AyushSaxena大神的英文原创作品 set::erase in C++ STL。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。