Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //In user.cpp
- /*User will include the header file
- */
- #include "Vector.h"
- #include <cmath>
- #include <stdexcept>
- #include <iostream>
- #include <initializer_list>
- #include <Vector>
- #include <thread>
- using namespace std;
- double sqrt_sum(Vector<double>& v)
- {
- double sum = 0;
- for (int i = 0; i != v.size(); ++i)
- sum+=sqrt(v[i]);
- return sum;
- }
- void f(Vector<int>& v)
- {
- //
- try {
- v[v.size()]=10;
- } catch (out_of_range) {
- cout<<"Out Of Range\n";
- }
- }
- void write(const Vector<string>& vs) //Vector of some strings
- {
- for ( int i = 0 ; i!= vs.size(); ++i)
- cout << vs[i] << '\n';
- }
- Vector<string> g()
- //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.
- {
- Vector<string> x(1000);
- Vector<string> y(1000);
- Vector<string> z(1000);
- //...
- z = x ; //we get a copy
- y = std::move(x); //we get a move
- //just before return we have z,y pointing to a Vector<string> and x nullptr
- return z; //we get a move
- //after return z is moved and it holds no elements
- }
- void f2( Vector<string> & vs)
- {
- for (auto& s: vs)
- cout << s << '\n';
- }
- void f3(vector<string> &vs)
- {
- for (auto& s: vs)
- cout << s << '\n';
- }
- int main()
- {
- Vector<string> i(10);//Resource acquisition is initialization. RAII
- //f(i);
- cout<<"DONE\n";
- return 0;
- }
- //In Vector.h
- template<typename T>
- class Vector{
- public:
- Vector(int s); // Constructor: establish invariant, acquire resources
- ~Vector(){ delete[] elem;} // destructor: release resources
- //copy nad move operations...
- Vector(const Vector& a); // copy constructor
- Vector& operator=(const Vector& a); // copy assignment
- Vector(Vector&& a); // move constructor
- Vector& operator =(Vector&& a); // move assignment
- T& operator[] (int);
- const T& operator[](int i ) const;
- int size() const;
- T* begin(Vector<T>& a);
- T* end(Vector<T>& b);
- Vector operator+(const Vector& a);
- Vector(std::initializer_list<T>);
- private:
- T* elem; //pointer to the elements
- int sz; //the number of elements
- };
- //In Vector.cpp
- #include "Vector.h"//get the interface
- #include <cmath>
- #include <stdexcept>
- #include <iostream>
- using namespace std;
- template <typename T>
- Vector<T>::Vector(int s) : elem{new T[s]} , sz{s}// construct a Vector
- {
- if(s<0) throw length_error{""};
- elem = new T[s];
- }
- //The members operator[]() and size() are said to override the corresponding members in the base class conatiner.
- template <typename T>
- T& Vector<T>::operator[](int i) //element access: subscripting
- {
- if(i<0 || size() <= i)
- throw out_of_range{"Vector<T>::operator[]"};
- return elem[i];
- }
- template <typename T>
- int Vector<T>::size() const {
- return sz;
- }
- template <typename T>
- Vector<T>::Vector(std::initializer_list<T> lst)
- :elem{new T[lst.size()]}, sz{static_cast<int>(lst.size())}
- {
- copy(lst.begin(), lst.end(), elem);
- }
- template <typename T>
- Vector<T>::Vector(const Vector& a) //copy constructor
- :elem{new T[a.sz]}, //allocate space for elements
- sz{a.sz}
- {
- for( int i = 0 ; i!=sz; ++ i) //copy elements
- elem[i] =a.elem[i];
- }
- template <typename T>
- Vector<T>& Vector<T>::operator=(const Vector &a) //copy assignment
- {
- T* p = new T[a.sz];
- for(int i =0 ; i != a.sz; ++i)
- p[i] = a.elem[i];
- delete[] elem; //delete old elements
- elem = p;
- sz = a.sz;
- return *this;
- }
- template <typename T>
- Vector<T>::Vector(Vector&& a) //move constructor
- :elem{a.elem}, //"grab the elements" from a
- sz{a.sz}
- {
- a.elem = nullptr; //now a has no elements
- a.sz = 0 ;
- }
- template <typename T>
- Vector<T>& Vector<T>::operator =( Vector &&a) //move assignment
- {
- elem = a.elem;
- sz = a.sz;
- a.elem = nullptr;
- a.sz = 0;
- return *this;
- }
- //To support the range-for loop for our Vector, we must define sutiable begin() and end() functions:
- template <typename T>
- T* Vector<T>::begin(Vector<T>& x)
- {
- return & x[0];
- }
- template <typename T>
- T* Vector<T>::end(Vector<T>& x)
- {
- return x.begin()+x.size(); //pointer to one past last element.
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement