C++类模板example

C++类模板example

  • 这里主要是参考了Essential C++书籍中的做法,对Matrix类做了一个简单的实现
  • 主要涉及的内容有:类模板、函数模板、操作运算符重载,友元函数等

注意

  • 有些运算符可以有多种实现方式,比如+*,可以实现类内的一元操作符重载,也可以以友元函数的形式实现二元操作符的重载(这里采用的方式)
  • 在类中声明友元函数模板时,需要重新使用一个模板名,否则会出现unresolved externals的错误。
  • 类模板的实现,脱离具体的使用是无法单独编译等,因此一般不建议将其声明和实现分开写,否则需要在include的时候#include "*.cpp"。因此建议将实现或者定义写在.h中类定义的外面,可以参考:https://blog.csdn.net/hemingliang1987/article/details/9473697
  • 具体的注意事项在程序中也有对应的说明

代码

  • MyMatrix.h

    #ifndef MY_MATRIX_H
    #define MY_MATRIX_H
    
    #include <cstdlib>
    #include <string>
    #include <iostream>
    #include <ostream>
    #include <assert.h>
    #include <time.h>
    #include <algorithm>
    #include <functional>
    #include <iterator>
    #include <fstream>
    #include <vector>
    #include <typeinfo>
    using namespace std;
    
    template<typename elemType>
    class MyMatrix
    {
        // 需要将单独定义友元函数的模板,直接使用elemType会出现错误
        template<typename T>
        friend MyMatrix<T> operator+ (const MyMatrix<T>&, const MyMatrix<T>&);
        template<typename T>
        friend MyMatrix<T> operator* (const MyMatrix<T>&, const MyMatrix<T>&);
    public:
        MyMatrix(int row, int col) 
            : _row(row), _col(col)
        {
            int size = _row*_col;
            _matrix = new elemType[ size ];
            for (int i = 0; i < size; i++)
            {
                _matrix[i] = elemType();
            }
        }
    
        MyMatrix(const MyMatrix<elemType>& matrix);
    
        MyMatrix<elemType>& operator=(const MyMatrix<elemType>& m1);
    
        void operator+= (const MyMatrix<elemType>&);
    
        ~MyMatrix()
        {
            delete[] _matrix;
        }
    
        const int rows() const { return _row; }
        const int cols() const { return _col; }
    
        elemType& operator() (int row, int col)
        {
            return _matrix[ row*_col + col ];
        }
    
        const elemType& operator() (int row, int col) const
        {
            return _matrix[row*_col + col];
        }
        ostream& print(ostream&) const;
    
    private:
        int _col;
        int _row;
        elemType* _matrix;
    };
    
    //
    // 函数定义需要和类的定义放在一起,否则无法编译通过
    //
    template<typename elemType>
    ostream& operator<<(ostream& os, const MyMatrix<elemType>& m)
    {
        return m.print(os);
    }
    
    template<typename elemType>
    ostream& MyMatrix<elemType>::print(ostream& os) const
    {
        int col = cols();
        int size = col*rows();
        for (int i = 0; i < size; i++)
        {
            if (i % col == 0)
                os << endl;
            os << _matrix[i] << " ";
        }
        os << endl;
        return os;
    }
    
    
    template<typename elemType>
    MyMatrix<elemType>::MyMatrix(const MyMatrix<elemType>& matrix)
    {
        _col = matrix._col;
        _row = matrix._row;
        _matrix = new elemType[_row*_col];
        int num = _col*_row;
        for (int i = 0; i < num; i++)
            _matrix[i] = matrix._matrix[i];
    }
    
    template<typename elemType>
    MyMatrix<elemType>& MyMatrix<elemType>::operator= (const MyMatrix<elemType>& matrix)
    {
        // 申请成功才会进行之前元素的删除
        if (this != &matrix)
        {
            delete[]_matrix;
            _col = matrix._col;
            _row = matrix._row();
            int num = _col*_row;
            _matrix = new elemType[_col*_row];
            for (int i = 0; i < num; i++)
                _matrix[i] = matrix._matrix[i];
        }
        return *this;
    }
    
    template<typename elemType>
    void MyMatrix<elemType>::operator+= (const MyMatrix<elemType>& m)
    {
        assert(_row == m.rows() && _col == m.cols());
        int size = _row*_col;
        for (int i = 0; i < size; i++)
            _matrix[i] += m._matrix[i];
    }
    
    template<typename elemType>
    MyMatrix<elemType> operator+ (const MyMatrix<elemType>& m1, const MyMatrix<elemType>& m2)
    {
        assert(m1.cols() == m2.cols() && m1.rows() == m2.rows());
        MyMatrix<elemType> m(m1);
        m += m2;
        return m;
    }
    
    template<typename elemType>
    MyMatrix<elemType> operator* (const MyMatrix<elemType>& m1, const MyMatrix<elemType>& m2)
    {
        assert(m1.cols() == m2.rows());
        MyMatrix<elemType> m(m1.rows(), m2.cols());
        for (int i = 0; i < m.rows(); i++)
        {
            for (int j = 0; j < m.cols(); j++)
            {
                for (int k = 0; k < m1.cols(); k++)
                {
                    m(i, j) += m1(i, k) * m2(k, j);
                }
    
            }
        }
        return m;
    }
    
    #endif
    
  • main.cpp

    #include "MyMatrix.h"
    using namespace std;
    typedef long long ll;
    
    void testMatrix()
    {
        MyMatrix<double> m1(4,4);
        for (int i = 0; i < m1.rows(); i++)
            for (int j = 0; j < m1.cols(); j++)
                m1(i, j) = i*m1.cols() + j;
        cout << "m1" << m1;
    
        MyMatrix<double> m2(m1);
        cout << "m2" << m2;
        MyMatrix<double> m3 = m1 + m2;
        cout << "m3" << m3;
        MyMatrix<double> m4 = m3 * m2;
        cout << "m4" << m4;
    }
    
    int main()
    {
        // 按照时间来初始化种子,保证每次结果不一样
        srand( (int)time(0) );
        //cout << rand() << endl;
    
        testMatrix();
    
        system("pause");
        return 0;
    }
    
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

littletomatodonkey

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值