练习STL,练习的网址戳我
Vector
- Declaration:定义
vector<int>v; (creates an empty vector of integers)
- Size:求大小
int size=v.size();
- Pushing an integer into a vector:存入数值
v.push_back(x);(where x is an integer.The size increases by 1 after this.)
- Popping the last element from the vector:删掉最后一个值
v.pop_back(); (After this the size decreases by 1)
- Sorting a vector:排序
sort(v.begin(),v.end()); (Will sort all the elements in the vector)
这个函数比较像数组,但是它开的范围比数组大。
练习题的代码
#include<iostream>
#include<vector>
#include<cstdio>
#include<algorithm>
using namespace std;
int n;
vector<int>v;
int main()
{
cin>>n;
int t;
for(int i=0;i<n;i++)
{
cin>>t;
v.push_back(t);
}
sort(v.begin(),v.end());
cout<<v[0];
for(int i=1;i<n;i++)
cout<<" "<<v[i];
cout<<endl;
return 0;
}