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
    评论
### 回答1: curl c example\ 是一个使用curl命令行工具的示例。curl是一个功能强大的用于与服务器进行通信的工具,支持多种协议,包括HTTP、FTP、TELNET等。 在这个示例中,c是curl命令的参数之一,它用来指定curl发送请求时使用的HTTP方法。c后面的example\是一个URL,用来指定要发送请求的目标服务器的地址。 例如,如果我们运行curl c example.com,那么curl将使用GET方法发送请求到example.com服务器,并返回服务器的响应。 除了c参数,curl还提供了许多其他参数,可以用来定制请求的行为。例如,可以使用-d参数来指定要发送的数据,使用-H参数来自定义请求头,使用-o参数来将服务器的响应保存到一个文件中等等。 在实际使用中,我们可以根据需要构建不同的curl命令,来实现各种不同的功能,如获取网页内容、上传文件、下载文件等等。 总之,curl c example\是一个使用curl的示例,通过它我们可以学习和理解如何使用curl命令行工具来进行网络通信。 ### 回答2: curl是一种用于与服务器进行通信的命令行工具。它是基于网络传输协议进行数据传输的工具之一。使用curl命令可以通过HTTP、HTTPS、FTP、SMTP等协议发送请求并获取响应。 在上述问题中,curl c example\ 是一个curl命令的示例。其中,c是curl命令的一个选项,用来指定请求的方式为POST。example则是请求的目标URL,\是转义字符,用于处理目标URL中可能存在的特殊字符。 这个命令的含义是使用POST方式向example发送请求。具体而言,curl将会将请求发送到example所表示的服务器,并期望获得服务器返回的响应。 需要注意的是,例子中使用了转义字符\来处理URL中可能的特殊字符,这是为了确保URL的正确解析,从而保证请求能够成功发送到目标服务器。 总结:curl命令是一种用于与服务器进行通信的命令行工具,可以通过不同的协议发送请求并获取响应。在上述问题中,curl c example\表示使用POST方式向example发送请求,并借助转义字符\处理目标URL中的特殊字符。 ### 回答3: curl是一个命令行工具,用于发送HTTP请求并显示响应。c是curl的一个选项,用于指定要发送请求的HTTP方法。example是示例的名称,而\则是转义字符,用于对后面的字符进行转义。由于在命令行中使用curl时,空格通常用于分隔不同的参数和选项,因此使用转义字符\来转义空格,以确保正确解析整个参数。因此,"curl c example\"的意思是使用curl发送一个HTTP请求,使用c选项指定HTTP方法,并将请求发送到示例的URL地址上。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

littletomatodonkey

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

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

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

打赏作者

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

抵扣说明:

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

余额充值