姓名
- 王胤皓
AC 记录
题目:
思路
用数组进行操作太难,而这些操作可以再 STL 中的 vector 容器,有 insert 和 erase 函数,所以非常方便。
vector 下标从
0
0
0 开始,所以所有操作都要
−
1
-1
−1。
操作 1 1 1
使用 vector 中的 insert 函数,用法:第一个是位置的地址,就是 vector.begin()+……
,第二个是插入的值,在此题中,应使用以下语句:v.insert(v.begin()+pos-1,x);
。
操作 2 2 2
使用 vector 中的 erase 函数,用法:第一个是删除的起始位置的地址,就是 vector.begin()+……
,第二个是删除的结束位置的地址,也就是 vector.begin()+……
,在此题中,应使用以下语句:v.begin()+pos-1,v.begin()+pos
。
操作 3 3 3
直接更改即可,语句:v[pos-1]=x;
。
操作 4 4 4
当然就是输出啦,输出 v[pos-1]
。
最终,输出长度,然后,输出 vector 里面的元素就可以啦,是不是非常简单~
代码
#include<bits/stdc++.h>
using namespace std;
int main(){
int n,m;
cin>>n>>m;
vector<int> v;
for(int i=1; i<=n; i++){
int x;
cin>>x;
v.push_back(x);
}
while(m--){
int t;
cin>>t;
if(t==1){
int pos,x;
cin>>pos>>x;
v.insert(v.begin()+pos-1,x);
}
if(t==2){
int pos;
cin>>pos;
v.erase(v.begin()+pos-1,v.begin()+pos);
}
if(t==3){
int pos,x;
cin>>pos>>x;
v[pos-1]=x;
}
if(t==4){
int pos;
cin>>pos;
cout<<v[pos-1]<<endl;
}
}
cout<<v.size()<<endl;
for(auto it:v){
cout<<(it)<<" ";
}
return 0;
}