Advertisement
SecRez

std::vector

Aug 24th, 2016
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.88 KB | None | 0 0
  1. #ifndef __STDVECTOR__
  2. #define __STDVECTOR__
  3.  
  4. #include <iostream>
  5.  
  6. using namespace std;
  7.  
  8. template <typename T>
  9.     class StdVector{
  10.         private:
  11.             T *buffer;
  12.             unsigned int capacity;
  13.         public:
  14.             //Constructor.
  15.             StdVector(){
  16.                 capacity=0;
  17.                 buffer=new T[capacity];
  18.             }
  19.             //Copy constructor.
  20.             StdVector(const StdVector &asv){
  21.                 int i;
  22.                
  23.                 capacity=asv.getCapacity();
  24.                 buffer=new T[asv.getCapacity()];
  25.                 for (i=0; i<capacity; i++){
  26.                     buffer[i]=asv[i];
  27.                 }
  28.             }
  29.             //Destructor.
  30.             ~StdVector(){
  31.                 delete []buffer;
  32.             }
  33.             void push_back(T obj){
  34.                 StdVector oldSV(*this);
  35.                 int i;
  36.                
  37.                 capacity++;
  38.                 delete []buffer;
  39.                 buffer=new T[capacity];
  40.                 for (i=0; i<oldSV.getCapacity(); i++){
  41.                     buffer[i]=oldSV[i];
  42.                 }
  43.                 buffer[i]=obj;
  44.             };
  45.             T getBuffer() const{
  46.                 if (capacity==0){
  47.                     throw exception();
  48.                 }
  49.                 return *buffer;
  50.             };
  51.             T &operator[](int index) const{
  52.                 if (index>=capacity){
  53.                     //Out of range.
  54.                     throw exception();
  55.                 }
  56.                 else{
  57.                     return buffer[index];
  58.                 }
  59.             }
  60.             StdVector &operator=(const StdVector &obj){
  61.                 capacity=obj.getCapacity();
  62.                 delete []buffer;
  63.                 buffer=new T[capacity];
  64.                 buffer=obj.getBuffer();
  65.                 return *this;
  66.             }
  67.             unsigned int getCapacity() const{
  68.                 return capacity;
  69.             };
  70.     };
  71.  
  72. #endif
  73.  
  74. int main(){
  75.     try{
  76.         StdVector<int> test;
  77.         StdVector<string> test2;
  78.         unsigned int i;
  79.        
  80.         test.push_back(5);
  81.         test.push_back(4);
  82.         test.push_back(3);
  83.         test.push_back(2);
  84.         test.push_back(1);
  85.         test.push_back(0);
  86.         test.push_back(-1);
  87.         test.push_back(-2);
  88.         test.push_back(-3);
  89.         test.push_back(-4);
  90.         test.push_back(-5);
  91.         for (i=0; i<test.getCapacity(); i++){
  92.             cout << test[i] << endl;
  93.         }
  94.         test2.push_back("Hello");
  95.         test2.push_back(" ");
  96.         test2.push_back("World");
  97.         test2.push_back(".");
  98.         cout << "---------------" << endl;
  99.         for (i=0; i<test2.getCapacity(); i++){
  100.             cout << test2[i];
  101.         }
  102.         cout << endl;
  103.     }
  104.     catch(...){
  105.         cout << "Exception." << endl;
  106.     }
  107.     return 0;
  108. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement