clear函数
语法:
1 |
void clear(); |
clear()清除所有元素
源代码
1 2 3 4 5 6 7 8 9 |
/** * Erases all elements in a %set. Note that this function only erases * the elements, and that if the elements themselves are pointers, the * pointed-to memory is not touched in any way. Managing the pointer is * the user's responsibilty. */ void clear() { _M_t.clear(); } |
测试代码
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
// 梁笔记 // https://zouzhongliang.com #include <iostream> #include <set> using namespace std; int main() { set<int> s1; s1.insert(10); s1.insert(12); s1.insert(13); s1.insert(9); cout<<"s1集合中元素数量:"<<s1.size()<<endl; s1.clear(); cout<<"s1集合中元素数量:"<<s1.size()<<endl; } |
测试结果:
1 2 |
s1集合中元素数量:4 s1集合中元素数量:0 |