练习STL,练习的网址戳我
Vector
- erase(int position):删除一个
Removes the element present at position.
Ex: v.erase(v.begin()+4); (erases the fifth element of the vector v)
删除第五个元素 - erase(int start,int end):删除连续多个
Removes the elements in the range from start to end inclusive of the start and exclusive of the end.
Ex:v.erase(v.begin()+2,v.begin()+5);(erases all the elements from the third element to the fifth element.)
删除第三个到第五个
练习题的代码
#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#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);
}
int x;
int p,q;
cin>>x;
cin>>p>>q;
v.erase(v.begin()+x-1);
v.erase(v.begin()+p-1,v.begin()+q-1);
cout<<v.size()<<endl;
for(int i=0;i<v.size();i++)
{
if(i==0)
cout<<v[i];
else
cout<<" "<<v[i];
}
cout<<endl;
return 0;
}