要清空C++的set容器,可以使用clear()
成員函數。這是一個示例:
#include <iostream>
#include <set>
int main() {
std::set<int> my_set = {1, 2, 3, 4, 5};
std::cout << "Before clearing: ";
for (int num : my_set) {
std::cout << num << " ";
}
std::cout << std::endl;
my_set.clear(); // 清空容器
std::cout << "After clearing: ";
for (int num : my_set) {
std::cout << num << " ";
}
std::cout << std::endl;
return 0;
}
輸出結果:
Before clearing: 1 2 3 4 5
After clearing: