C++ vector 容器淺析[02] ~ 清除容器中所以數據
C++ vector 容器淺析[02] ~ 清除容器中所以數據
資料來源: https://www.runoob.com/w3cnote/cpp-vector-container-analysis.html
純C++線上編譯測試: https://www.tutorialspoint.com/compile_cpp_online.php
純C 線上編譯測試: https://www.tutorialspoint.com/compile_c_online.php
#include <string> #include <vector> #include <iostream> using namespace std; int main() { vector<int>obj; for(int i=0;i<10;i++)//push_back(elem)在数组最后添加数据 { obj.push_back(i); } cout << "obj.size:" << obj.size() << endl; cout << "obj print:" <<endl; for(int i=0;i<obj.size();i++) { cout<<obj[i]<<"," ; } cout << endl; obj.clear();//清除容器中所以数据 cout << endl; cout << "obj.clear()"<< endl; cout << "obj.size:" << obj.size() << endl; cout << "obj print:" <<endl; for(int i=0;i<obj.size();i++) { cout<<obj[i]<<endl; } return 0; }