Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
//
// Created by bing on 2021/3/11.
//
#include <iostream>
#include <ctime>
class Vector {
private:
double* elem; // elem points to an array of sz doubles
int sz;
public:
Vector(int s); // constructor: acquire resources
~Vector() { delete[] elem; } // destructor: release resources
Vector(const Vector& a); // copy constructor
Vector& operator=(const Vector& a); // copy assignment operator
double& operator[](int i) const { return elem[i]; };
int size() const {return sz;};
};
Vector::Vector(int s)
{
// std::cout << "non-default constructor"<<std::endl;
elem = {new double[s]};
sz =s;
for (int i=0; i!=s; ++i) // initialize elements
elem[i]=0;
}
Vector::Vector(const Vector& a) // copy constructor
:elem{new double[a.sz]},
sz{a.sz}
{
std::cout << "copy constructor"<<std::endl;
for (int i=0; i!=sz; ++i) // copy elements
elem[i] = a.elem[i];
}
Vector& Vector::operator=(const Vector& a) // copy assignment operator
{
std::cout << "copy assignment operator"<<std::endl;
std::clock_t c_start = std::clock(); // measure cpu time
double* p = new double[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;
std::clock_t c_end = std::clock();
std::cout << "CPU clock: "<< (c_end-c_start) << std::endl;
return *this;
}
Vector operator+(const Vector& a, const Vector& b)
{
if (a.size()!=b.size()){
//throw Vector_with_no_move_size_mismatch{};
}
Vector res(a.size());
for (int i=0; i!=a.size(); ++i)
res[i]=a[i]+b[i];
return res;
}
class Vector_with_move {
private:
double* elem; // elem points to an array of sz doubles
int sz;
public:
Vector_with_move(int s); // constructor: acquire resources
~Vector_with_move() { delete[] elem; } // destructor: release resources
Vector_with_move(const Vector_with_move& a); // copy constructor
Vector_with_move& operator=(const Vector_with_move& a); // copy assignment operator
Vector_with_move(Vector_with_move&& a); // move constructor
Vector_with_move& operator=(Vector_with_move&& a); // move assignment
double& operator[](int i) const { return elem[i]; };
int size() const {return sz;};
};
Vector_with_move::Vector_with_move(int s)
{
// std::cout << "non-default constructor"<<std::endl;
elem = {new double[s]};
sz =s;
for (int i=0; i!=s; ++i) // initialize elements
elem[i]=0;
}
Vector_with_move::Vector_with_move(const Vector_with_move& a) // copy constructor
:elem{new double[a.sz]},
sz{a.sz}
{
std::cout << "copy constructor"<<std::endl;
for (int i=0; i!=sz; ++i) // copy elements
elem[i] = a.elem[i];
}
Vector_with_move& Vector_with_move::operator=(const Vector_with_move& a) // copy assignment operator
{
std::cout << "copy assignment operator"<<std::endl;
double* p = new double[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;
}
Vector_with_move::Vector_with_move(Vector_with_move&& a) // move constructor
:elem{a.elem}, // "grab the elements" from a
sz{a.sz}
{
std::cout << "move constructor"<<std::endl;
a.elem = nullptr; // now a has no elements
a.sz = 0;
}
Vector_with_move& Vector_with_move::operator=(Vector_with_move&& a) // move assignment operator
{
std::cout << "move assignment operator"<<std::endl;
std::clock_t c_start = std::clock();
delete[] elem; // delete old elements
elem = a.elem ;
sz = a.sz;
a.elem = nullptr; // now a has no elements
a.sz = 0;
std::clock_t c_end = std::clock();
std::cout << "CPU clock: "<< (c_end-c_start) << std::endl;
return *this;
}
Vector_with_move operator+(const Vector_with_move& a, const Vector_with_move& b)
{
if (a.size()!=b.size()){
//throw Vector_size_mismatch{};
}
Vector_with_move res(a.size());
for (int i=0; i!=a.size(); ++i)
res[i]=a[i]+b[i];
return res;
}
void test_copy_assignment(int num_of_elem){
Vector r(num_of_elem);
Vector a(num_of_elem);
Vector b(num_of_elem);
Vector c(num_of_elem);
std::cout << "### start to copy!" << std::endl;
r = a+b+c;
std::cout << "!!! copy finished! Vector size: " << r.size() << std::endl;
}
void test_move_assignment(int num_of_elem){
Vector_with_move r(num_of_elem+1);
Vector_with_move a(num_of_elem);
Vector_with_move b(num_of_elem);
Vector_with_move c(num_of_elem);
std::cout << "### start to move!" << std::endl;
r= a+b+c;
std::cout << "!!! move finished! Vector_with_move size: " << r.size() << std::endl;
}
int main(){
int x=10000;
test_copy_assignment(x);
test_move_assignment(x);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment