首页 > 其他 > 详细

用vector实现矩阵, vector传参必须用模板泛型

时间:2017-03-23 01:29:51      阅读:503      评论:0      收藏:0      [点我收藏+]
#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];
		}
	}
};

  

用vector实现矩阵, vector传参必须用模板泛型

原文:http://www.cnblogs.com/blacop/p/6602674.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!