C ++ set erase()函數(shù)用于從set容器中刪除與給定鍵關(guān)聯(lián)的單個(gè)元素或元素范圍([first,last))。因此,將通過刪除元素的數(shù)量來減小大小。
void erase (iterator position); //C++ 11 之前 size_type erase (const value_type& val); //C++ 11 之前 void erase (iterator first, iterator last); //C++ 11 之前 iterator erase (const_iterator position); //從 C++ 11開始 size_type erase (const value_type& val); //從 C++ 11開始 iterator erase (const_iterator first, const_iterator last); //從 C++ 11開始
position:迭代器,指向要從集合中刪除的單個(gè)元素。
val:要從集合中刪除的值。
first:要擦除范圍的開始。
last:要擦除范圍的末尾。
它返回一個(gè)指向已刪除元素的下一個(gè)元素的迭代器,或者返回已刪除元素的數(shù)量。
erase(position):攤銷常數(shù)。
erase (val):容器大小的對(duì)數(shù)。
erase(first,last):第一個(gè)和最后一個(gè)的距離是線性的。
迭代器,引用和指向該函數(shù)刪除的元素的指針均無效。
所有其他迭代器,指針和引用均保持其有效性。
容器已修改。
刪除的元素將被修改。盡管同時(shí)訪問其他元素是安全的,但在容器中進(jìn)行迭代范圍并不安全。
此函數(shù)不會(huì)引發(fā)異常。
如果指定了無效的范圍或位置,則將導(dǎo)致未定義的行為。
讓我們看一個(gè)簡單的示例,該示例通過迭代器擦除元素。
#include <iostream>
#include <set>
using namespace std;
int main () {
set<int> myset;
set<int>::iterator it;
myset = {
10,20,30
}
;
cout<<"刪除元素之前: \n";
for (it=myset.begin(); it!=myset.end(); ++it)
cout << *it << '\n';
it=myset.find('b');
myset.erase (*it);
// erasing by iterator
cout<<"\n刪除元素后: \n";
for (it=myset.begin(); it!=myset.end(); ++it)
cout << *it << '\n';
return 0;
}輸出:
刪除元素之前: 10 20 30 刪除元素后: 10 20 30
在上面的示例中,元素被迭代器刪除。
讓我們看一個(gè)簡單的示例,用給定的鍵值刪除集合中的元素:
#include <iostream>
#include <set>
using namespace std;
int main ()
{
set<int> myset;
set<int>::iterator it;
myset = {10, 20, 30, 40};
cout<<"刪除元素之前: \n";
for (it=myset.begin(); it!=myset.end(); ++it){
cout << *it<< '\n';
}
myset.erase (30);//刪除指定值
cout<<"\n刪除元素之后: \n";
for (it=myset.begin(); it!=myset.end(); ++it){
cout << *it<< '\n';
}
return 0;
}輸出:
刪除元素之前: 10 20 30 40 刪除元素之后: 10 20 40
在上面的示例中,erase (value)函數(shù)使用集合中的值30。
讓我們看一個(gè)簡單的示例,以給定范圍擦除元素:
#include <iostream>
#include <set>
using namespace std;
int main () {
set<int> myset;
set<int>::iterator it;
myset = {
10, 20, 30
};
cout<<"刪除元素之前: \n";
cout<<"Size is: "<<myset.size()<<'\n';
for (it=myset.begin(); it!=myset.end(); ++it){
cout << *it << '\n';
}
myset.erase ( myset.begin () , myset.end () );
// 擦除指定范圍
cout<<"\n刪除元素之后: \n";
cout<<"Size is: "<<myset.size();
for (it=myset.begin(); it!=myset.end(); ++it){
cout << *it << '\n';
}
return 0;
}輸出:
刪除元素之前: Size is: 3 10 20 30 刪除元素之后: Size is: 0
在上面的示例中,使用了Erase(first,last)函數(shù)來擦除具有給定范圍(即開始到結(jié)束)的元素。
讓我們看一個(gè)簡單的示例,以刪除集合中的所有奇數(shù):
#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};
// 從m刪除所有奇數(shù)
cout<<"刪除奇數(shù)后,元素是:\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 << ", ";
}
}輸出:
刪除奇數(shù)后,元素是: 2, 4, 6, 8, 10, 12, 14,
在上面的示例中,所有奇數(shù)均已刪除,并顯示偶數(shù)。
讓我們看另一個(gè)實(shí)例:
#include <set>
#include <string>
#include <iostream>
#include <iterator> // next()和prev()輔助函數(shù)
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;
// 填入一些數(shù)據(jù)進(jìn)行測試,一次一個(gè)
s1.insert("Bob");
s1.insert("Robert");
s1.insert("Bert");
s1.insert("Rob");
s1.insert("Bobby");
cout << "集合s1的起始數(shù)據(jù)為:" << endl;
printset(s1);
// 第一個(gè)成員函數(shù)刪除給定位置的元素
s1.erase(next(s1.begin()));
cout << "刪除第2個(gè)元素后,集合s1為:" << endl;
printset(s1);
// 使用初始化器列表一次填入一個(gè)要測試的數(shù)據(jù)
myset s2{ "meow", "hiss", "purr", "growl", "yowl" };
cout << "集合s2的起始數(shù)據(jù)為:" << endl;
printset(s2);
// 第二個(gè)成員函數(shù)刪除元素
// 范圍 [First, Last)
s2.erase(next(s2.begin()), prev(s2.end()));
cout << "刪除中間元素后,集合s2為:" << endl;
printset(s2);
myset s3;
// 使用emplace一次填入一個(gè)要測試的數(shù)據(jù)
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 << "集合s3的開始數(shù)據(jù)為:" << endl;
printset(s3);
// 第三個(gè)成員函數(shù)刪除具有給定鍵的元素
myset::size_type count = s3.erase("E#");
// 第三個(gè)成員函數(shù)也返回被刪除的元素?cái)?shù)量
cout << "從s3中刪除的元素?cái)?shù)量為: " << count << "." << endl;
cout << "刪除鍵為“ E#”的元素后,集合s3為:" << endl;
printset(s3);
}輸出:
集合s1的起始數(shù)據(jù)為: [Bert] [Bob] [Bobby] [Rob] [Robert] size() == 5 刪除第2個(gè)元素后,集合s1為: [Bert] [Bobby] [Rob] [Robert] size() == 4 集合s2的起始數(shù)據(jù)為: [growl] [hiss] [meow] [purr] [yowl] size() == 5 刪除中間元素后,集合s2為: [growl] [yowl] size() == 2 集合s3的開始數(shù)據(jù)為: [A] [A#] [B] [C] [C#] [D] [D#] [E] [E#] [F] [F#] [G] [G#] size() == 13 從s3中刪除的元素?cái)?shù)量為: 1. 刪除鍵為“ E#”的元素后,集合s3為: [A] [A#] [B] [C] [C#] [D] [D#] [E] [F] [F#] [G] [G#] size() == 12