在 C++ 语言的 STL 标准模板库 中 , set 集合容器 是一个有序的集合 , 存储的元素值都是唯一的 , 不重复的 ;
调用 set 集合容器的 erase 函数 , 可以删除 集合容器 中指定值 的元素 ;
上述 set#erase 函数原型如下 :
size_type erase (const key_type& k);
// set 集合容器
// 初始化列表中的顺序会自动排序
set<int> se{ 9, 5, 7 };
// 删除元素 7
se.erase(7);
代码示例 :
#include "iostream"
using namespace std;
#include "set"
// 声明遍历打印 set 集合容器的函数
void printS(set<int>& se);
int main() {
// set 集合容器
// 初始化列表中的顺序会自动排序
set<int> se{ 9, 5, 7 };
// 删除元素 7
se.erase(7);
// 打印 set 集合容器
printS(se);
// 控制台暂停 , 按任意键继续向后执行
system("pause");
return 0;
};
// 遍历打印 set 集合容器元素
void printS(set<int>& se) {
// 遍历 set 集合容器
for (set<int>::iterator it = se.begin(); it != se.end(); it++)
{
cout << *it << " ";
}
// 回车换行
cout << endl;
}
执行结果 : 在代码中 元素 7 被删除了 , 只打印出了 5 , 9 两个元素 ;
5 9 Press any key to continue . . .
set#erase 函数 还可以传入一个 指向指定元素位置的 迭代器 对象 , 作为参数 , 删除该迭代器指向的元素 ;
函数原型如下 :
iterator erase (iterator position);
// set 集合容器
// 初始化列表中的顺序会自动排序
set<int> se{ 9, 5, 7 };
// 删除集合容器中第二个元素
se.erase(++se.begin());
完整代码示例 :
#include "iostream"
using namespace std;
#include "set"
// 声明遍历打印 set 集合容器的函数
void printS(set<int>& se);
int main() {
// set 集合容器
// 初始化列表中的顺序会自动排序
set<int> se{ 9, 5, 7 };
// 打印 set 集合容器
printS(se);
// 删除集合容器中第二个元素
se.erase(++se.begin());
// 打印 set 集合容器
printS(se);
// 控制台暂停 , 按任意键继续向后执行
system("pause");
return 0;
};
// 遍历打印 set 集合容器元素
void printS(set<int>& se) {
// 遍历 set 集合容器
for (set<int>::iterator it = se.begin(); it != se.end(); it++)
{
cout << *it << " ";
}
// 回车换行
cout << endl;
}
执行结果 :
5 7 9 5 9 Press any key to continue . . .
调用 set#erase 函数 , 可以传入两个迭代器参数 , 这两个迭代器划定了本集合容器的一个范围 , 执行该函数可删除该范围的所有元素 ;
函数原型如下 :
iterator erase (iterator first, iterator last);
// set 集合容器
// 初始化列表中的顺序会自动排序
set<int> se{ 9, 5, 2, 7 };
// 删除集合容器中第二个元素和第三个元素
se.erase(++se.begin(), --se.end());
完整代码示例 :
#include "iostream"
using namespace std;
#include "set"
// 声明遍历打印 set 集合容器的函数
void printS(set<int>& se);
int main() {
// set 集合容器
// 初始化列表中的顺序会自动排序
set<int> se{ 9, 5, 2, 7 };
// 打印 set 集合容器
printS(se);
// 删除集合容器中第二个元素和第三个元素
se.erase(++se.begin(), --se.end());
// 打印 set 集合容器
printS(se);
// 控制台暂停 , 按任意键继续向后执行
system("pause");
return 0;
};
// 遍历打印 set 集合容器元素
void printS(set<int>& se) {
// 遍历 set 集合容器
for (set<int>::iterator it = se.begin(); it != se.end(); it++)
{
cout << *it << " ";
}
// 回车换行
cout << endl;
}
执行结果 :
2 5 7 9 2 9 Press any key to continue . . .
调用 set 集合容器的 clear 函数 , 可以删除容器中的所有元素 ;
函数原型如下 :
void clear();
该函数没有参数和返回值 , 使用示例如下 :
// set 集合容器
// 初始化列表中的顺序会自动排序
set<int> se{ 9, 5, 2, 7 };
// 删除集合容器中所有元素
se.clear();
完整代码示例 :
#include "iostream"
using namespace std;
#include "set"
// 声明遍历打印 set 集合容器的函数
void printS(set<int>& se);
int main() {
// set 集合容器
// 初始化列表中的顺序会自动排序
set<int> se{ 9, 5, 2, 7 };
// 打印 set 集合容器
printS(se);
// 删除集合容器中所有元素
se.clear();
// 打印 set 集合容器
printS(se);
// 控制台暂停 , 按任意键继续向后执行
system("pause");
return 0;
};
// 遍历打印 set 集合容器元素
void printS(set<int>& se) {
// 遍历 set 集合容器
for (set<int>::iterator it = se.begin(); it != se.end(); it++)
{
cout << *it << " ";
}
// 回车换行
cout << endl;
}
执行结果 :
2 5 7 9
Press any key to continue . . .