要清空C++的set容器,可以使用clear()
成员函数。这是一个示例:
#include#include int main() { std::set 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: