zoukankan      html  css  js  c++  java
  • 用vector实现矩阵, vector传参必须用模板泛型

    #pragma once
    #include "stdafx.h"
    
    //用vector实现矩阵, vector传参必须用模板泛型
    template <typename Object>
    class Matrix {
    private:
    	//2维的矩阵,2维的vector数组,vector就是一种动态数组
    	vector<vector<Object>> array;
    public:
    	//constructor(), 填充数组(行数)
    	Matrix(int rows, int cols) :array(rows) {
    		for (int i = 0; i < rows; i++)
    			//resize(),改变当前使用数据的大小,如果它比当前使用的大,者填充默认值
    			array[i].resize(cols);
    	}
    	//重载操作符[],实现索引器,常量引用传值
    	const vector<Object>& operator[](int row)const {
    		return array[row];
    	}
    	//重载操作符[],实现索引器,变量引用传值
    	vector<Object> & operator[](int row) {
    		return array[row];
    	}
    	//Length()
    	int numrows() const {
    		//array.Length()
    		return array.size();
    	}
    	//numcols()
    	int numcols() const {
    		//numrows() is true;
    		return numrows() ? array[0].size() : 0;
    	}
    	//deconstructor()
    	virtual ~Matrix() {}
    	//copy()
    	void copy(const Matrix<int>& from, Matrix<int>& to) {
    		for (int i = 0; i < to.numrows; i++) {
    			to[i] = from[i];
    		}
    	}
    };
    

      

  • 相关阅读:
    js 获得多个同name 的input输入框的值
    推荐系统
    异常检测
    降维——PCA主成分分析
    无监督学习——降维
    无监督学习——K-means聚类
    支持向量机——内核
    支持向量机背后的数学
    支持向量机——Large Margin Classifier
    支持向量机
  • 原文地址:https://www.cnblogs.com/blacop/p/6602674.html
Copyright © 2011-2022 走看看