在C++中,通过调用set容器的clear()方法来清空set容器中的所有元素。示例代码如下:
#include <iostream>
#include <set>
int main() {
std::set<int> mySet = {1, 2, 3, 4, 5};
// 清空set容器
mySet.clear();
// 输出set容器的大小,应该为0
std::cout << "Size of set after clearing: " << mySet.size() << std::endl;
return 0;
}
辰迅云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>
推荐阅读: 通过红黑树优化C++中的网络包分类和路由选择