练习Vector-2

练习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;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值