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

需求

目标是实现类似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;
}

结果

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

清欢_小铭

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

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

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

打赏作者

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

抵扣说明:

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

余额充值