clear函数
语法:
1 |
void clear(); |
clear() 清空所有元素
源代码
1 2 3 4 5 6 7 8 |
/** * Erases all the elements. 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() { erase(begin(), end()); } |
测试代码
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
// 梁笔记 // https://zouzhongliang.com #include <iostream> #include <vector> using namespace std; int main() { vector<int> v1(10, 0); vector<int> v2(10, 2); for(int i=0;i<v2.size();i++){ cout<<v2.at(i)<<","; } cout<<endl; v2.clear();//清空 cout<<"v2内元素个数" << v2.size()<<endl; } |
测试结果:
1 2 |
2,2,2,2,2,2,2,2,2,2, v2内元素个数0 |