hackerrank>Dashboard>C++>STL>Vector-Erase
来源:互联网 发布:java线程结束 编辑:程序博客网 时间:2024/06/05 16:26
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 ofintegers 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 integersand 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
61 4 6 2 8 922 4
Sample Output
31 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}
先删除所给下标的值,下标变动后,删除所给范围内下标的值,输出STL的size,与STL存储的值。
#include <cmath>#include <cstdio>#include <vector>#include <iostream>#include <algorithm>#include <map>#define MAX 1e9+8using namespace std;int main(){ int N; vector<int>vec1; while(~scanf("%d",&N)) { while(N--) { int t; scanf("%d",&t); vec1.push_back(t); } int n,m; cin>>n; vec1.erase(vec1.begin()+n-1); vector<int>::iterator it; cin>>n>>m; m=m-n; while(m--) vec1.erase(vec1.begin()+n-1); cout<<vec1.size()<<endl; cout<<*vec1.begin(); for(it=vec1.begin()+1; it!=vec1.end(); it++) cout<<' '<<*it; cout<<endl; } return 0;}
- hackerrank>Dashboard>C++>STL>Vector-Erase
- hackerrank>Dashboard>C++>STL>Vector-Sort
- hackerrank>Dashboard>C++>STL>Deque-STL
- hackerrank>Dashboard>C++>STL> Maps-STL
- hackerrank>Dashboard>C++>STL>Sets-STL
- hackerrank>Dashboard>C++>STL>Lower Bound-STL
- hackerrank>Dashboard>C++>STL>Print Pretty
- C++STL:erase
- STL vector的erase操作问题
- STL vector的erase操作问题
- STL vector erase 笔试 曼恒数字
- STL vector的erase操作问题
- STL vector的erase操作问题
- STL Vector remove()和erase()的使用
- STL Vector remove()和erase()的使用
- STL vector erase 笔试 曼恒数字
- STL 中vector的erase问题
- STL vector 的 erase(); 函数漏洞?
- xslt/xpath对不存在属性的判断问题
- **ANDROID** activity使用和相关的重要类
- windows下mysql每天定时备份数据库
- 解决placeholder兼容性问题
- C. New Year Book Reading codeforces
- hackerrank>Dashboard>C++>STL>Vector-Erase
- VSYNC信号传递流向
- 使用git命令提交代码
- C中&与I的使用
- 数据交互
- Mybatis resultMap空值映射问题解决
- 容斥原理:HDU-4135Co-prime
- **ANDROID**Intent和IntentFilter的进行通信
- 数据库没有备份无法打开处理方法之二dul