# [TOJ 1214] data structure Exercise - linear table operation (vector Implementation)

Description

Please define a linear table. You can insert an element before a location, delete the element in a location, clear all elements, and get the element in a location. You can do this by typing some commands on the keyboard. In this problem, the element of linear table is integer, and the first element position of linear table is 1. The maximum length of a linear table is 1000.

Input

The format of each command and related data is as follows:

Insert before a location: insert, next row is the number of inserted groups n, next row is the data of N rows, each row has two values, respectively representing the location and the inserted element value

Clear linear table: clear

Get the element of a location: getelem, the next line is the element location to get

Delete the element in a certain location: delete, and the next line is the location of the deleted element

When the command entered is exit, the program ends

Output

When the input command is getelem, please output the obtained element value,

When the entered command is delete, please output the element value to be deleted

Note that all elements take up one row

Sample input

insert
2
1 1
2 2
delete
1
clear
insert
2
1 3
2 4
getelem
2
exit

Sample output

1
4

```#include<vector>
#include<iostream>
#include<cstring>
using namespace std;
int main()
{
vector<int>v;
vector<int>::iterator it;
char a[15];
int i,p,x,n,s;                                //s Count as
while(scanf("%s",a)!=EOF)
{
s=1;                                      //Because vector Count from 0, and we usually count elements from 1
if(strcmp(a,"insert")==0)
{
scanf("%d",&n);
for(i=1;i<=n;i++)
{
cin>>p>>x;                        //p Location No. p One, x It is the first. p Values of elements
v.insert(v.begin()+p-1,x);        //Because vector Count from 0, so insert the p Location of elements requires-1
}
}
else if(strcmp(a,"clear")==0)
{
v.clear();
}
else if(strcmp(a,"exit")==0)
break;
else if(strcmp(a,"getelem")==0)
{
cin>>n;
for(it=v.begin();it!=v.end();it++,s++)
if(s==n)
{
cout<<*it<<endl;
break;
}
}
else if(strcmp(a,"delete")==0)
{
cin>>n;
for(it=v.begin();it!=v.end();s++)
if(s==n)                          //The first n Element
{
cout<<*it<<endl;
it=v.erase(it);               //Delete element, return value points to the next location of the deleted element
break;
}
else it++;                        //Point to next position
}
}
}```

Keywords: C++

Added by drummerboy on Sat, 04 Apr 2020 08:16:21 +0300