stl习题练习

Vector-Erase

You are provided with a vector of integers. Then, you are given queries. For the first query, you are provided with integer, which denotes a position in the vector. The value at this position in the vector needs to be erased. The next query consists of integers denoting a range of the positions in the vector. The elements which fall under that range 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:N212

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.
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}
————————————————
版权声明:本文为CSDN博主「veit sun」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/qq_33962143/article/details/113251137

#include <iostream>
#include <algorithm>
#include <vector>
#include <string>


using namespace std;

int main()
{
	int n;
	cin>>n;
	vector<int> v(n);
	for(int i=0; i<n; i++)
	{
		cin>>v[i];
	}
	int x,a,b;
	cin>>x;
	cin>>a;
	cin>>b;
	v.erase(v.begin()+x-1);
	v.erase(v.begin()+a-1,v.begin()+b-1);
	cout<<v.size()<<endl;
	for(int i=0; i<v.size()-1; i++)
		cout<<v[i]<<" ";
	cout<<v[v.size()-1]<<endl;
	return 0;
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值