Vector-Erase

Vector-Erase

You are given a vector of integers.Then you are given queries.First query consists of integer denoting the position in the vector which should be removed.Next query consists of integers denoting the range of the positions in the vector that should be removed. The second query is performed on the updated vector which we get after performing the first query.
The following are some useful vector functions:

· 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.)

Input Format

The first line of the input contains an integer .The next line contains space separated integers(1-based index).The third line contains a single integer ,denoting the position of an element that should be removed from the vector.The fourth line contains two integers and denoting the range that should be erased from the vector inclusive of a and exclusive of b.

Constraints


Output Format

Print the size of the vector in the first line and the elements of the vector after the two erase operations in the second line separated by space.

Sample Input

6

1 4 6 2 8 9

2

2 4

Sample Output

3

1 8 9

Explanation

The first query is to erase the 2nd element in the vector, which is 4. Then, modifed vector is {1 6 2 8 9}, we want to remove the range of 2~4, which means the 2nd and 3rd elements should be removed. Then 6 and 2 in the modified vector are removed and we finally get {1 8 9}

百度翻译:

给你一个整数向量,然后给你一个查询,第一个查询是整数,表示向量中的位置,它应该被删除,下一个查询由整数表示向量中应该删除的向量的范围。第二个查询是在执行第一查询后得到的更新向量执行的。

以下是一些有用的向量函数:

擦除(int位置)

移除当前位置的元素。

例如:v.eraseV. begin() + 4);(擦除矢量V第五元)

擦除(int启动,INT结束):

移除从开始到结束包含开始和结尾的范围内的元素。

例如:v.erase(诉诉begin() begin() + 2+ 5);(除从第三元到第五元的所有元素。)

输入格式

输入的第一行包含一个整数。下一行包含整数(一维索引),第三行是一个整数,表示一个元素,应该从向量移除的位置。第四行包含两个整数,表示应该删除从载体包括一个独家B.范围

约束

输出格式

在第一行的矢量大小和向量的元素后,在第二行分隔两个擦除操作空间。

样本输入

4 6 2 8 9

2 4

示例输出

1 8 9

解释

第一个查询是清除vector中的第二个元素,它是4。然后,修改向量{ 1 6 2 8 9 },我们要删除2 ~ 4范围内,这意味着第二和第三的元素应该被删除。然后62在修改后的矢量被删除,我们终于得到{ 1 8 9 }

 

题意:删除第一个输入的t位置的元素。重新排。再删除从t1t2位置之间的所有元素。

 

 

#include<cstdio>

#include<iostream>

#include<algorithm>

#include<vector>

using namespace std;

int main()

{

    int n,x,sum=0;

    cin>>n;

    vector<int>v;

    int i;

    for(i=0;i<n;i++)

    {

        cin>>x;

        v.push_back(x);

    }

    int t;

    cin>>t;

    v.erase(v.begin()+t-1);

    int t1,t2;

    cin>>t1>>t2;

    v.erase(v.begin()+t1-1,v.begin()+t2-1);

    sum=n-1-(t2-t1);

    cout<<sum<<endl;

    for(i=0;i<sum;i++)

    {

        cout<<v[i]<<" ";

    }

    cout<<endl;

    return 0;

}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值