fork download
  1. #include<iostream>
  2. using namespace std;
  3. #ifndef __vector__cpp__
  4. #define __vector__cpp__
  5.  
  6. template <class T>
  7. class vector_reverse_iterator
  8. {
  9. T *curr;
  10. public:
  11. vector_reverse_iterator(T *c=0) {curr=c;}
  12. vector_reverse_iterator<T> &operator=(vector_reverse_iterator<T> it)
  13. {
  14. this->curr=it.curr;
  15. return *this;
  16. }
  17. vector_reverse_iterator<T> operator++()//++curr
  18. {
  19. curr--;
  20. return curr;
  21. }
  22. vector_reverse_iterator<T> operator++(int)//++curr
  23. {
  24. vector_reverse_iterator<T> tmp=curr;
  25. curr--;
  26. return tmp;
  27. }
  28. T &operator*() {return *curr;}
  29. bool operator!=(vector_reverse_iterator<T> t) {return curr!=t.curr;}
  30. };
  31.  
  32. template<class T>
  33. class Vector
  34. {
  35. private:
  36. int cap,num;
  37. T *buff;
  38. public:
  39. Vector() {cap=num=0;buff=0;}
  40. Vector(int k,T x) {cap=num=k; buff=new T[k]; for(int i=0;i<k;i++) buff[i]=x;}
  41. ~Vector() {if(buff) delete[]buff;}
  42. int capacity() {return cap;}
  43. int size() {return num;}
  44. bool empty() {return num==0;}
  45. void pop_back() {if(num>0) num--;}
  46. void extend(int newcap)
  47. {
  48. if(newcap<cap) return;
  49. cap=newcap;
  50. T *temp=new T[cap];
  51. for(int i=0;i<num;i++) temp[i]=buff[i];
  52. if(buff) delete []buff;
  53. buff= temp;
  54. }
  55. T &back() {return buff[num-1];}
  56. void push_back(T x)
  57. {
  58. if(num==cap) extend(cap*2+5);
  59. buff[num++]=x;
  60. }
  61. T &operator[](int k) {return buff[k];}
  62. void insert(int k,T x)
  63. {
  64. if(cap==num) extend(cap*2+5);
  65. for(int i=num-1;i>=k;i--) buff[i+1]=buff[i];
  66. buff[k]=x;
  67. num++;
  68. }
  69. Vector &operator=(Vector<T> V)
  70. {
  71. this->num=V.num;
  72. this->cap=V.cap;
  73. if(cap)
  74. {
  75. this->buff=new T[cap];
  76. for(int i=0;i<num;i++) this->buff[i]=V.buff[i];
  77. }
  78. else this->buff=0;
  79. return *this;
  80. }
  81. typedef T *iterator;
  82. iterator begin() {return buff;}
  83. iterator end() {return buff+num;}
  84. typedef vector_reverse_iterator<T> reverse_iterator;
  85. reverse_iterator rbegin() {return reverse_iterator(buff+num-1);}
  86. reverse_iterator rend() {return reverse_iterator(buff-1);}
  87. };
  88. #endif
  89.  
  90. int main()
  91. {
  92. Vector<int> V(7,6);
  93. cout<<"\nVector ban dau:";for(int i=0;i<V.size();i++) cout<<V[i]<<" ";
  94. for(int i=0;i<V.size();i++) V[i]=i*i;
  95. cout<<"\nVector binhphuong:";for(int i=0;i<V.size();i++) cout<<V[i]<<" ";
  96. V.push_back(11);
  97. V.push_back(13);
  98. V.insert(2,20);
  99. cout<<"\nVector :";for(int i=0;i<V.size();i++) cout<<V[i]<<" ";
  100. cout<<"\nDuyet :";for(Vector<int>::iterator it=V.begin();it!=V.end();it++) cout<<*it<<"\t";
  101. cout<<"\nDuyet auto : ";for(auto x:V) cout<<x<<" ";
  102. cout<<"\nNguoc : "; for(auto it=V.rbegin();it!=V.rend();it++) cout<<*it<<" ";
  103. }
Success #stdin #stdout 0.01s 5324KB
stdin
Standard input is empty
stdout
Vector ban dau:6 6 6 6 6 6 6 
Vector binhphuong:0 1 4 9 16 25 36 
Vector :0 1 20 4 9 16 25 36 11 13 
Duyet :0	1	20	4	9	16	25	36	11	13	
Duyet auto : 0 1 20 4 9 16 25 36 11 13 
Nguoc : 13 11 36 25 16 9 4 20 1 0