#include <iostream>
#include <vector>
#include<algorithm>
using namespace std;
int main(){
vector<int> height;
height.resize(10);
for(int j=0;j<10;j++)cout<<height[j]<<" ";
cout<<endl;
height.resize(10);
for (int i=0;i<10;i++)height[i]=i;
for(int j=0;j<10;j++)cout<<height[j]<<" ";
cout<<endl;
height.resize(5);
for(int i=0;i<10;i++)height.push_back(2*i);
for(int j=0;j<15;j++)cout<<height[j]<<" ";
cout<<endl;
height.resize(10);
for(int i=0;i<10;i++)height.push_back(height[i]*5);
for(int j=0;j<20;j++)cout<<height[j]<<" ";
cout<<endl;
return 0;
}
见代码 :
resize()为设置vector大小,默认设置值为0;
之后用push_back会往后填充数,用height[i]可以改变值;