需求

目标是实现类似STL的矩阵库,可以实现一维和二维矩阵,老师制作了一部分,学生负责完善。模板化的安全数组成为STL兼容的容器。
By now you should have a working templatized safe array class from the midterm and safematrix project. Build upon the templatized safe array (1- dimensional NOT the safe “matrix”) add an iterator and the other necessary typedefs required to make the templatized safearray an STL compatible container, refer back to the class videos to see examples of this. Once you add the necessary code to use STL make sure to test your code by using STL find on your templatized safe array. Your safe array will be graded on being properly templatized, having working constructors and operators based on the original example, and being compatible with STL using the STL find as a test.

要点

迭代器本质上通过类中类实现

代码

模板类通常放在hpp文件中,作业较小,全部写在一个文件中。

#include<iostream>
#include <cstdlib>
#include <cassert>
#include<fstream>
#include<sstream>
using namespace std;
template <class T>
class SA;//一维矩阵
template <class T>
class SM;//一维矩阵二维矩阵
template <class T>
ostream& operator<<(ostream& os, SA<T> s);
template <class T>//输入输出函数
ostream& operator<< (ostream& os, SM<T> s);
template < class T > class SA {private:int low, high;//一维数组有效的下标范围T* p;//数组的首地址
public:SA() {//无参构造low = 0;high = -1;p = NULL;}SA(int l, int h) {//有参构造,两个参数代表有效下标位置if ((h - l + 1) <= 0) {//判断下表值是否合法cout << "constructor bounds error" << endl;exit(1);}low = l;high = h;p = new T[h - l + 1];}SA(int i) {//有参构造,下表从零开始low = 0;high = i - 1;p = new T[i];}SA(const SA& s) {//拷贝构造函数int size = s.high - s.low + 1;p = new T[size];for (int i = 0; i < size; i++)p[i] = s.p[i];low = s.low;high = s.high;}// destructor~SA() {//析构delete[]p;}T& operator[](int i) {//重载[]运算符,可以访问其中的元素if (i < low || i > high) {cout << "index " << i << " out of range" << endl;exit(1);}return p[i - low];}SA& operator=(const SA& s) {//=重载,相当于拷贝赋值if (this == &s)return *this;delete[]p;int size = s.high - s.low + 1;p = new T[size];for (int i = 0; i < size; i++)p[i] = s.p[i];low = s.low;high = s.high;return *this;}
class iterator{//类中类实现迭代器friend class SA;//友元保证迭代器能被对应容器访问
private:T* it;//迭代器本质上是对指针的管理,其核心数据是指针
public:iterator(T* p){//带参数构造函数it=p;}~iterator(){}、、析构没写,应该进行内存释放bool operator>(iterator i){//比较运算符重载if(this->it > i.it)return true;return false;}bool operator<(iterator i){if(this->it < i.it)return true;return false;}bool operator==(iterator i){if(this->it == i->it)return true;return false;}bool operator!=(iterator i){if(this->it != i.it)return true;return false;}//重载递增符号前缀和后缀不同iterator operator++(int){T* res= ++this->it;return iterator(res);}T operator*(){//本职工作——指针return *(this->it);}int operator-(iterator i){//做差return (this->it - i.it);}
};iterator operator+(int i) {if(i<=high)return iterator(&p[low + i]);return iterator(nullptr);}iterator begin() {return iterator(&p[low]);}iterator end() {return iterator(&p[high + 1]);}iterator find(iterator it1, iterator it2, T v) {if (it1 > it2)return end();if (it1.it < &p[low])return end();if (it1.it > &p[high])return end();for (it1;  it1 < it2; it1++) {if (*it1 == v) return it1;}return end();}friend ostream& operator<< <T>(ostream& os, SA<T> s);
};template <class T>
ostream& operator<<(ostream& os, SA < T > s) {int size = s.high - s.low + 1;for (int i = 0; i < size; i++)os << s.p[i] << endl;return os;
}template < class T > class SM {private:int row_low;int row_high;int col_low;int col_high;SA < SA < T > > matrix;public:SM(int rows, int cols) {if (rows <= 0 || cols <= 0) {cout << "Please enter a valid row and columns size" << endl;exit(1);}row_low = 0;row_high = rows - 1;col_low = 0;col_high = cols - 1;matrix = SA < SA < T > >(rows);for (int j = 0; j < rows; j++)matrix[j] = SA < T >(cols);}SM() {}SM(int row_min, int row_max, int col_min, int col_max) {if ((row_max - row_min + 1) <= 0) {cerr << "constructor error in Matrix bounds definition" << endl;exit(1);}row_low = row_min;row_high = row_max;col_low = col_min;col_high = col_max;matrix = SA < SA < T > >(row_min, row_max);for (int i = row_min; i <= (row_max); i++)matrix[i] = SA < T >(col_min, col_max);}SM(int square_size) {row_low = 0;row_high = square_size - 1;col_low = 0;col_high = square_size - 1;matrix = SA < SA < T > >(square_size);for (int j = 0; j < square_size; j++)matrix[j] = SA < T >(square_size);}//destructor~SM() {}SA < T >& operator[](int i) {if (i < row_low || i > row_high) {cout << "index " << i << " out of range in Matrix" << endl;exit(1);}return matrix[i];}/*Matrix Multiplication*/SM < T > operator*(SM& s) {if ((col_high - col_low + 1) != (s.row_high - s.row_low + 1)) {return 0;}int rows = (row_high - row_low + 1);int cols = (s.col_high - s.col_low + 1);SM < int >result(rows, cols);for (int r = 0; r < rows; r++) {for (int c = 0; c < cols; c++) {result[r][c] = 0;}}for (int r = 0; r < rows; r++) {for (int c = 0; c < cols; c++) {for (int i = 0; i < (s.row_high - s.row_low + 1); i++) {result[r][c] += ((*this)[r + row_low][i + col_low]) * (s[i + s.row_low][c + s.col_low]);}}}return result;}friend ostream& operator<< <T>(ostream& os, SM<T> s);};
template <class T>
ostream& operator<<(ostream& os, SM < T > s) {for (int i = s.row_low; i <= s.row_high; i++) {for (int j = s.col_low; j <= s.col_high; j++) {os << s.matrix[i][j] << " ";}os << endl;}return os;
}
int main() {int n = 6;SA<double> a(n);//SA<int> b(3, 5);//SM<int> m1(3, 3);//SM<int> m2(3, 3);for (int i = 0; i < n; i++) {a[i] = (double)i+0.5;}cout << a;double  number = 0.0;while (number != -1){cout << "enter num (-1) to end";cin >> number;if (number != -1){SA<double>::iterator position = a.find(a.begin(), a.end(), number);if (position != a.end()){cout << "found at position " << (position - a.begin()) << endl;}else{cout << number << " not found." << endl;}}}return 0;
}

结果

安全数组-简易矩阵容器-STL及迭代器仿写作业相关推荐

  1. C++ STL与迭代器

    将容器类模板实例化时,会指明容器中存放的元素是什么类型的:可以存放基本类型的变量,也可以存放对象. 对象或基本类型的变量被插入容器中时,实际插入的是对象或变量的一个复制品. STL 中的许多算法(即函 ...

  2. vector容器与iterator迭代器

    vector容器 vector是同一种类型的对象的集合,每个对象都有一个对应的整数索引值.和string对象一样,标准库负责管理存储元素的相关内存.我们把vector称为容器,是因为它可以包含其他对象 ...

  3. STL中迭代器的作用,有指针为何还要迭代器

    请你来说一下STL中迭代器的作用,有指针为何还要迭代器 参考回答: 1.迭代器 Iterator(迭代器)模式又称Cursor(游标)模式,用于提供一种方法顺序访问一个聚合对象中各个元素, 而又不需暴 ...

  4. MATLAB学习——数据类型(结构体、数组、单元数组、map容器类型)

    本篇文章对Matlab中的剩余数据类型进行介绍,它们分别为结构体类型.数组类型.单元数组类型.map容器类型. 结构体类型 与C语言中的结构体类型相似,Matlab中的结构体也可以通过字段存储多个不同 ...

  5. C++中STL各个迭代器详解

    1.自C++11起可以用range-based for循环来所有元素,但有时并不需要处理所有元素,此时可以使用迭代器. std::vector<int> vec {1,2,3,4,5,6, ...

  6. 二维数组练习--矩阵的加法和乘法

    数组的练习示例展示: package arrayList; /*** 矩阵的集中运算法则:求和,求积,求逆矩阵,转置矩阵......* @author Drew**/ public class Arr ...

  7. python 读取图片成为一维数组_python+opencv 图像的数组和矩阵操作

    在调用opencv的imread函数读取图像时,我们得到的其实是一个类型为numpy.ndarray的n维数组.这个数组的维度是[height,width,3],它是由每个像素的RGB通道的灰度值组成 ...

  8. C++创建二维数组和矩阵

    参考: http://bbs.csdn.net/topics/330125254 http://see.xidian.edu.cn/cpp/biancheng/view/44.html 必须让电脑知道 ...

  9. numpy np.matmul()(两个数组的矩阵乘积)

    from multiarray def matmul(a, b, out=None): # real signature unknown; restored from __doc__"&qu ...

最新文章

  1. 人工智能及其应用(第5版).蔡自兴-1-5章课后习题。【部分无答案】
  2. SQL Server 环形缓冲区(Ring Buffer) -- 介绍
  3. Oracle RESETLOGS 和 NORESETLOGS 区别说明
  4. 数据结构与算法 / 平衡二叉树(AVL树)
  5. 开启AngularJS 1.X的学习之路(1)
  6. PostgreSQL常用SQL
  7. ORACLE PL/SQ入门
  8. 异常来自 HRESULT:0x800A01A8
  9. Solr服务器搭建与简单使用
  10. 网页CSS常用中英文字体收集
  11. 清明节偷偷训练“熊猫烧香”,结果我的电脑为熊猫“献身了”!
  12. C1083:无法打开源文件
  13. ansiblea基本使用
  14. Hubstudio指纹浏览器和MaxProxy代理的配置教程
  15. 读书笔记:《死去之前,都是人生》
  16. 全球与中国铝合金窗型材市场规模预测与产销前景调研报告2022版
  17. html5页面风格,H5页面的设计风格有哪些?
  18. office2010安装出现错误1935的解决方法
  19. [js插件开发教程]定制一个手风琴插件(accordion)
  20. 计算机重启遇到你的账户已被停用,win10系统提示administrator您的账户已被停用的解决方法...

热门文章

  1. mysql strict_mysql 严格模式 Strict Mode说明(转)
  2. 第三课--AMP架构双核应用程序开发和软中断处理(一)
  3. 夹娃娃_夹娃娃的实用性方法 抓娃娃可以用什么技巧
  4. 0-1总体分布下的参数假设检验示例一(SPSS实现)
  5. 文档处理工具库——Apache POI的使用
  6. 跨媒体检索(关联)之基于CCA的方法大总结
  7. esri-leaflet入门教程(3)-自定义底图
  8. SpringBoot工程发布
  9. Direct3D顶点结构使用总结
  10. mysql sql数据排名_查询数据排名情况SQL