c++中set容器關(guān)閉的方法是什么

c++
小億
85
2024-05-17 14:27:13

在C++中,通過(guò)調(diào)用set容器的clear()方法來(lái)清空set容器中的所有元素。示例代碼如下:

#include <iostream>
#include <set>

int main() {
    std::set<int> mySet = {1, 2, 3, 4, 5};

    // 清空set容器
    mySet.clear();

    // 輸出set容器的大小,應(yīng)該為0
    std::cout << "Size of set after clearing: " << mySet.size() << std::endl;

    return 0;
}

0