Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<bits/stdc++.h>
- using namespace std;
- int size=1,counter,in;
- int* tab = new int[1];
- void alloc(int res){
- int* helptab = new int[counter];
- for(int i = 0;i < counter;i++)
- helptab[i] = tab[i];
- delete[] tab;
- tab = new int[res];
- for(int i = 0;i < counter;i++)
- tab[i] = helptab[i];
- delete[] helptab;
- size = res;
- }
- void pb(int x){
- if(counter == size)
- alloc(2*counter);
- tab[counter] = x;
- counter++;
- }
- void pop(){
- if(counter == 0){
- cout<<"No elements left, sorry!\n";
- return;
- }
- counter--;
- if(counter < size/4)
- alloc(size/2);
- }
- void t(int x){
- if(x<counter && x>=0)
- cout<<"The "<<x<<"-th element is "<<tab[x]<<"\n";
- else
- cout<<"This element doesn't exist\n";
- }
- int main() {
- cout<<"Welcome to probably the best vector in the universe!\nYour commands are as follows:\npb x = add x at the end\npop = delete the last element\nt x = check the x-th position(if it exists)\nexit = terminates the program\n";
- string comm = "a";
- while(comm != "exit"){
- cin>>comm;
- if(comm == "pb"){
- cin>>in;
- pb(in);
- }
- if(comm == "pop")
- pop();
- if(comm == "t"){
- cin>>in;
- t(in);
- }
- }
- cout<<"Farewell!\n";
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement