Advertisement
Guest User

Untitled

a guest
Aug 3rd, 2014
129
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. //In user.cpp
  2. /*User will include the header file
  3.  */
  4. #include "Vector.h"
  5. #include <cmath>
  6. #include <stdexcept>
  7. #include <iostream>
  8. #include <initializer_list>
  9. #include <Vector>
  10. #include <thread>
  11.  
  12. using namespace std;
  13.  
  14. double sqrt_sum(Vector<double>& v)
  15. {
  16.     double sum = 0;
  17.     for (int i = 0; i != v.size(); ++i)
  18.         sum+=sqrt(v[i]);
  19.     return sum;
  20. }
  21. void f(Vector<int>& v)
  22. {
  23.     //
  24.     try {
  25.         v[v.size()]=10;
  26.     } catch (out_of_range) {
  27.         cout<<"Out Of Range\n";
  28.     }
  29. }
  30. void write(const Vector<string>& vs)    //Vector of some strings
  31. {
  32.     for ( int i = 0 ; i!= vs.size(); ++i)
  33.         cout << vs[i] << '\n';
  34. }
  35. Vector<string> g()
  36. //Where the programmer knows that a valie will not be used again, but the compiler can't be expected to be smart enough to figure that out, the programmer can be specific.
  37. {
  38.     Vector<string> x(1000);
  39.     Vector<string> y(1000);
  40.     Vector<string> z(1000);
  41.     //...
  42.     z = x ;             //we get a copy
  43.     y = std::move(x);   //we get a move
  44.     //just before return we have z,y pointing to a Vector<string> and x nullptr
  45.     return z;           //we get a move
  46.     //after return z is moved and it holds no elements
  47. }
  48. void f2( Vector<string> & vs)
  49. {
  50.     for (auto& s: vs)
  51.         cout << s << '\n';
  52. }
  53. void f3(vector<string> &vs)
  54. {
  55.     for (auto& s: vs)
  56.         cout << s << '\n';
  57. }
  58. int main()
  59. {
  60.     Vector<string> i(10);//Resource acquisition is initialization. RAII
  61.     //f(i);
  62.     cout<<"DONE\n";
  63.     return 0;
  64. }
  65. //In Vector.h
  66. template<typename T>
  67. class Vector{
  68. public:
  69.     Vector(int s);                          // Constructor: establish invariant, acquire resources
  70.     ~Vector(){ delete[] elem;}              // destructor: release resources
  71.  
  72.     //copy nad move operations...
  73.     Vector(const Vector& a);                // copy constructor
  74.     Vector& operator=(const Vector& a);     // copy assignment
  75.    
  76.     Vector(Vector&& a);                     // move constructor
  77.     Vector& operator =(Vector&& a);         // move assignment
  78.    
  79.     T& operator[] (int);
  80.     const T& operator[](int i ) const;
  81.     int size() const;
  82.    
  83.      T* begin(Vector<T>& a);
  84.      T* end(Vector<T>& b);
  85.    
  86.     Vector operator+(const Vector& a);
  87.    
  88.     Vector(std::initializer_list<T>);
  89.    
  90.    
  91. private:
  92.     T* elem;   //pointer to the elements
  93.     int sz;         //the number of elements
  94. };
  95.  
  96.  
  97. //In Vector.cpp
  98.  
  99. #include "Vector.h"//get the interface
  100. #include <cmath>
  101. #include <stdexcept>
  102. #include <iostream>
  103.  
  104. using namespace std;
  105. template <typename T>
  106. Vector<T>::Vector(int s) : elem{new T[s]} , sz{s}// construct a Vector
  107. {
  108.     if(s<0) throw length_error{""};
  109.     elem = new T[s];
  110. }
  111. //The members operator[]() and size() are said to override the corresponding members in the base class conatiner.
  112. template <typename T>
  113. T& Vector<T>::operator[](int i) //element access: subscripting
  114. {
  115.     if(i<0 || size() <= i)
  116.         throw out_of_range{"Vector<T>::operator[]"};
  117.     return elem[i];
  118. }
  119. template <typename T>
  120. int Vector<T>::size() const {
  121.     return sz;
  122. }
  123. template <typename T>
  124. Vector<T>::Vector(std::initializer_list<T> lst)
  125.     :elem{new T[lst.size()]}, sz{static_cast<int>(lst.size())}
  126. {
  127.     copy(lst.begin(), lst.end(), elem);
  128. }
  129. template <typename T>
  130. Vector<T>::Vector(const Vector& a)     //copy constructor
  131. :elem{new T[a.sz]},            //allocate space for elements
  132. sz{a.sz}
  133. {
  134.     for( int i = 0 ; i!=sz; ++ i)   //copy elements
  135.         elem[i] =a.elem[i];
  136. }
  137. template <typename T>
  138. Vector<T>& Vector<T>::operator=(const Vector &a) //copy assignment
  139. {
  140.     T* p = new T[a.sz];
  141.     for(int i =0 ; i != a.sz; ++i)
  142.         p[i] = a.elem[i];
  143.     delete[] elem;                         //delete old elements
  144.     elem = p;
  145.     sz = a.sz;
  146.     return *this;
  147. }
  148. template <typename T>
  149. Vector<T>::Vector(Vector&& a)      //move constructor
  150.     :elem{a.elem},              //"grab the elements" from a
  151.     sz{a.sz}
  152. {
  153.     a.elem = nullptr;           //now a has no elements
  154.     a.sz = 0 ;
  155. }
  156. template <typename T>
  157. Vector<T>& Vector<T>::operator =( Vector &&a) //move assignment
  158. {
  159.     elem = a.elem;
  160.     sz = a.sz;
  161.     a.elem = nullptr;
  162.     a.sz = 0;
  163.     return *this;
  164. }
  165. //To support the range-for loop for our Vector, we must define sutiable begin() and end() functions:
  166. template <typename T>
  167.  T* Vector<T>::begin(Vector<T>& x)
  168. {
  169.     return & x[0];
  170. }
  171. template <typename T>
  172.  T* Vector<T>::end(Vector<T>& x)
  173. {
  174.     return x.begin()+x.size(); //pointer to one past last element.
  175. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement