Guest User

Untitled

a guest
Jul 17th, 2018
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.74 KB | None | 0 0
  1. /* in class code
  2.    03/26/2012
  3.    vector
  4. */
  5. #include <iostream>
  6. #include <vector>
  7. using namespace std;
  8.  
  9. class Vector{ // type is the pointer to the first element
  10. public:
  11.  Vector() : theSize(0), theCapacity(1), data(new int[theCapacity]){}
  12.  
  13.  explicit Vector(size_t initialSize) :
  14.  theSize(initialSize), theCapacity(initialSize), data(new int[theCapacity])
  15.  {
  16.   for (size_t i = 0; i < initialSize; ++i) data[i] = 0;
  17.   cerr << "Vector()\n";
  18.  }
  19.  
  20.  
  21.  // system provided default constructor does nothing
  22.  // garbage values..
  23.  
  24.  //methods:
  25.  //size
  26.  size_t size() const { return theSize;}
  27.  
  28.  void push_back(int n){
  29.   if (!theSize == theCapacity){
  30.    theCapacity*=2;
  31.    int* tempData = new int[theCapacity];
  32.    for (size_t i = 0; i < theSize; ++i){
  33.     tempData = data[i];
  34.    }
  35.    delete []  data; // tell the sytem that we are freeing up an array
  36.    data = tempData;
  37.    
  38.   }
  39.   data[theSize] = n;
  40.   ++ theSize;
  41.   //data[theSize ++];
  42.  }
  43.  
  44.  
  45.  //v.operator[](i)
  46.  int operator[](size_t index) const {return data[index];}
  47.  int& operator[](size_t index){return data[index];}
  48.  
  49.  
  50.  // copy control
  51.  // destructor
  52.  -Vector() {delete [] data;}
  53.  
  54.  // copy constructor
  55.  Vector(const Vector& rhs){
  56.   // Mionr bookkeeping
  57.   theSize = rhs.theSize;
  58.   theCapacity = rhs.theCapacity;
  59.   // Allocate the array
  60.   data = new int[theCapacity];
  61.  
  62.   // copy over the data
  63.   for (size_t i = 0; i < theSize; ++1){
  64.    data[i] = rhs.data[i];
  65.   }
  66.  }
  67.  // assignment operator
  68.  Vector& operator =(const Vector& rhs){
  69.   //0. self-assignment
  70.   //if(rhs == * this)
  71.   if (&rhs != this){
  72.   //1. free up
  73.    delete [] data;
  74.    // Mionr bookkeeping
  75.    theSize = rhs.theSize;
  76.    theCapacity = rhs.theCapacity;
  77.    // Allocate the array
  78.    data = new int[theCapacity];
  79.    
  80.    // copy over the data
  81.    for (size_t i = 0; i < theSize; ++1){
  82.     data[i] = rhs.data[i];
  83.    }
  84.   }
  85.    
  86.   //2. allocate
  87.   data = new int[theCapacity];
  88.   //3. copying the data
  89.    for (size_t i = 0; i < theSize; ++1){
  90.     data[i]=rhs.data[i];
  91.    }
  92.   // * this = Vector(rhs);// NOOOOO!!
  93.   // 4. return yourself
  94.   return *this;
  95.  }
  96.  
  97.  void pop_back(){
  98.   if (size_t > 0) -- theSize;
  99.  }
  100.  void clear{ the size = 0;}
  101.    
  102.  
  103.  
  104.  
  105.  
  106. private:
  107.  size_t theCapacity;
  108.  int* data;
  109.  size_t theSize;
  110.  
  111.  
  112.  // p[i] means *(p+i)
  113.  // pointer arithmetic
  114.  
  115. };
  116.  
  117. int main(){
  118.  Vector v;
  119.  cout << v.size() << endl;
  120.  v.push_back(17);
  121.  v.push_back(45);
  122.  v.push_back(53);
  123.  v.push_back(78);
  124.  for (size_t i = 0; i < v.size(); ++i){
  125.   cout << v[i] << endl;
  126.   //v.operator[](i)        
  127.  }
  128.  v[2] = 100;
  129.  
  130.  Vector anotherVector(v);
  131.  // Vector anotherVector = v;
  132.  // 1) pass by value
  133.  // 2) return by value
  134.  // 3) initialization
  135.  v.pop_back();
  136.  
  137.  v = 7;
  138.  v = vector(7);
  139.  
  140.  
  141.  
  142.  
  143. }
Add Comment
Please, Sign In to add comment