#include <iostream> #include <vector> using namespace std; int main () { vector<int> myvector; // set some initial content: for (int i=1;i<10;i++) myvector.push_back(i);//长度为9 myvector.resize(5);//重新设置长度为5,多余的元素删掉 myvector.resize(8,100);//重新设置长度为8,填充元素为100 myvector.resize(12);//默认填充的元素是0 cout << "myvector contains:"; for (int i=0;i<myvector.size();i++) cout << ' ' << myvector[i]; cout << ' '; return 0; }
输出
myvector contains: 1 2 3 4 5 100 100 100 0 0 0 0