Advertisement
Guest User

Untitled

a guest
Oct 19th, 2017
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.25 KB | None | 0 0
  1. #include<bits/stdc++.h>
  2. using namespace std;
  3.  
  4. int size=1,counter,in;
  5. int* tab = new int[1];
  6.  
  7. void alloc(int res){
  8. int* helptab = new int[counter];
  9. for(int i = 0;i < counter;i++)
  10. helptab[i] = tab[i];
  11. delete[] tab;
  12. tab = new int[res];
  13. for(int i = 0;i < counter;i++)
  14. tab[i] = helptab[i];
  15. delete[] helptab;
  16. size = res;
  17. }
  18.  
  19. void pb(int x){
  20. if(counter == size)
  21. alloc(2*counter);
  22.  
  23. tab[counter] = x;
  24. counter++;
  25. }
  26.  
  27. void pop(){
  28. if(counter == 0){
  29. cout<<"No elements left, sorry!\n";
  30. return;
  31. }
  32. counter--;
  33. if(counter < size/4)
  34. alloc(size/2);
  35. }
  36.  
  37. void t(int x){
  38. if(x<counter && x>=0)
  39. cout<<"The "<<x<<"-th element is "<<tab[x]<<"\n";
  40. else
  41. cout<<"This element doesn't exist\n";
  42. }
  43.  
  44. int main() {
  45. 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";
  46. string comm = "a";
  47. while(comm != "exit"){
  48. cin>>comm;
  49. if(comm == "pb"){
  50. cin>>in;
  51. pb(in);
  52. }
  53. if(comm == "pop")
  54. pop();
  55. if(comm == "t"){
  56. cin>>in;
  57. t(in);
  58. }
  59. }
  60. cout<<"Farewell!\n";
  61. return 0;
  62. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement