C++ vector 容器淺析[01] ~ 實例在容器最後移除和插入數據
C++ vector 容器淺析[01] ~ 實例在容器最後移除和插入數據
資料來源: 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(10,0);//最大容器为10,都初始化为0 for(int i=0;i<obj.size();i++) { cout<<obj[i]<<","; } cout<<"\n"<<endl; for(int i=0;i<10;i++)//push_back(elem)在数组最后添加数据 { obj.push_back(i); } for(int i=0;i<obj.size();i++) { cout<<obj[i]<<","; } cout<<"\n"<<endl; for(int i=0;i<5;i++)//去掉数组最后一个数据 { obj.pop_back(); } for(int i=0;i<obj.size();i++)//size()容器中实际数据个数 { cout<<obj[i]<<","; } return 0; }