繁体   English   中英

C ++-矩阵类复制构造函数

[英]C++ - Matrix class copy constructor

我试图建立一个Matrix类,并且想知道复制构造函数。 我已经阅读了复制和交换的习惯用法运算符重载 我无法确定哪个更有效:

解决方案1:

Matrix<T>::Matrix(const Matrix<T>& rhs) {
    Matrix<T> temp(rhs);
    this.swap(temp);
}

Matrix<T>::void swap(Matrix<T> rhs) 
: dx(rhs.dx), dy(rhs.dy) {
  allocArrays();
  for (int i=0; i<dx; ++i) {
    for (int j=0; j<dy; ++j) {
      p[i][j] = rhs.p[i][j];
    }
  }
}

或采取简单的路线并简单地使用

解决方案2:

Matrix::Matrix(const Matrix& rhs) 
: dx(m.dx), dy(m.dy) {
allocArrays();
  for (int i=0; i<dx; ++i) {
    for (int j=0; j<dy; ++j) {
      p[i][j] = rhs.p[i][j];
    }
  }
}

矩阵标题:

#ifndef MATRIX_H
#define MATRIX_H

#include <vector>

template <typename T> class Matrix {
private:
    std::vector<std::vector<T>> matrix;
    void allocArrays();  
    unsigned int dx, dy; // dimensions of x and y
    long **p; // pointer to pointer for long int

public:
    Matrix();
    ~Matrix();
    Matrix(const Matrix<T>& rhs);
    Matrix(unsigned xSize, unsigned ySize);
    Matrix<T>& operator=(const Matrix<T>& rhs);

    // Adding below code to showcase what else will be happening with the Matrices

    // Matrix mathematical operations                                                                                                                                                                                               
    Matrix<T> operator+(const Matrix<T>& rhs);
    Matrix<T>& operator+=(const Matrix<T>& rhs);
    Matrix<T> operator-(const Matrix<T>& rhs);
    Matrix<T>& operator-=(const Matrix<T>& rhs);
    Matrix<T> operator*(const Matrix<T>& rhs);
    Matrix<T>& operator*=(const Matrix<T>& rhs);
    Matrix<T> transpose();

    // Matrix/scalar operations                                                                                                                                                                                                     
    Matrix<T> operator+(const T& rhs);
    Matrix<T> operator-(const T& rhs);
    Matrix<T> operator*(const T& rhs);
    Matrix<T> operator/(const T& rhs);

    // Matrix/vector operations                                                                                                                                                                                                     
    std::vector<T> operator*(const std::vector<T>& rhs);
    std::vector<T> diag_vec();

    // Access the individual elements                                                                                                                                                                                               
    T& operator()(const unsigned& row, const unsigned& col);
    const T& operator()(const unsigned& row, const unsigned& col) const;
};

#include "matrix.cpp"
#endif

矩阵CPP文件:

#ifndef _MATRIX_CPP
#define _MATRIX_CPP

#include "matrix.h"
#include <iostream>

// Default Constructor 
Matrix<T>::Matrix() : Matrix(1, 1) {}

// Parameter Constructor                                                                                                                                                      
template<typename T>
Matrix<T>::Matrix(unsigned xSize, unsigned ySize) 
    : dx(xSize), dy(ySize) {

    allocArrays();

    for (int i = 0; i < dx; i++) {
        for (int j = 0; j < dy; j++) {
            p[i][j] = 0;
        }
    }
}

// Copy Constructor @ which is most effective way of doing this                                                                                                                                                          
template<typename T>
Matrix<T>::Matrix(const Matrix<T>& rhs) {}

Matrix<T>::void allocArrays() {
    p = new long*[dx];
    for (int i = 0; i < dx; i++)
        p[i] = new long[dy];
}

第一个解决方案意味着递归构造函数调用,将永远无法工作:

Matrix<T>::Matrix(const Matrix<T>& rhs) {
    Matrix<T> temp(rhs);
        ^^^^^^^^^^^^^^
    // Recursive call here
    this.swap(temp);
}

至于任何潜在的性能差异,请始终在一组不错的测试用例上使用探查器。 如果正确执行了交换,您将在任何体面的编译器中看到,与矩阵的分配/填充相比,为交换创建临时文件的成本可以忽略不计。

注意:

如您的链接所示,复制和交换习惯是由赋值运算符(调用复制构造函数)而不是复制构造函数本身实现的。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM