集是一種關聯容器,其中每個元素都必須是唯一的,因為元素的值可以標識它。盡管可以刪除並添加該元素的修改後的值,但是一旦將元素的值添加到集合中就無法對其進行修改。
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。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。