c++ 重设容器的长度(resize)

时间:2022-05-12 15:50:54
#include <iostream>
#include <vector>
using namespace std;
int main ()
{
vector<int> myvector; // set some initial content:
for (int i=;i<;i++) myvector.push_back(i);//长度为9 myvector.resize();//重新设置长度为5,多余的元素删掉
myvector.resize(,);//重新设置长度为8,填充元素为100
myvector.resize();//默认填充的元素是0 cout << "myvector contains:";
for (int i=;i<myvector.size();i++)
cout << ' ' << myvector[i];
cout << '\n'; return ;
}

输出

myvector contains: