实现一个大小固定的矩阵

#include <iostream>
#include <numeric>
#include <algorithm>
#include <cmath>
#include <valarray>

using namespace std;

template<class Value_T, unsigned int N>
class kvector
{
public:
    Value_T m[N];
    typedef Value_T value_type; 
    typedef Value_T* iterator;
    typedef const Value_T* const_iterator;
    typedef Value_T& reference;
    typedef const Value_T& const_reference; typedef size_t size_type;

    typedef kvector self;

    template<typename Iter_T>
    void copy(Iter_T first, Iter_T last) { copy(first, last, begin()); }
    iterator begin() { return m; }
    iterator end() { return m + N; }
    const_iterator begin() const { return m; }
    const_iterator end() const { return m + N; }
    reference operator[] (size_type n) { return m[n]; }
    const_reference operator[] (size_type n) const { return m[n]; }
    static size_type size() { return N; }

    self& operator+=(const self& x) 
    {
        for (int i = 0; i < N; ++i )
            m[i] += x.m[i];
        return *this;
    }
    self& operator-=(const self& x)
    {
        for (int i = 0; i < N; ++i)
            m[i] -= x.m[i];
        return *this;
    }

    self& operator=(value_type x)
    {
        fill(begin(), end(), x);
        return *this;
    }
    self& operator+=(value_type x)
    {
        for (int i = 0; i < N; ++i)
            m[i] += x;
        return *this;
    }
    self& operator-=(value_type x)
    {
        for (int i = 0; i < N; ++i)
            m[i] -= x;
        return *this;
    }
    self& operator*=(value_type x)
    {
        for (int i = 0; i < N; ++i)
            m[i] *= x;
        return *this;
    }
    self& operator/=(value_type x)
    {
        for (int i = 0; i < N; ++i)
            m[i] /= x;
        return *this;
    }
    self& operator%=(value_type x)
    {
        for (int i = 0; i < N; ++i)
            m[i] %= x;
        return *this;
    }
    self operator-()
    {
        self x;
        for (int i = 0; i < N; ++i)
            x.m[i] = -x.m[i];
        return x;
    }

    friend self operator+(self x, const self& y) { return x += y; }
    friend self operator-(self x, const self& y) { return x -= y; }
    friend self operator+(self x, value_type y) { return x += y; }
    friend self operator-(self x, value_type y) { return x -= y; }
    friend self operator*(self x, value_type y) { return x *= y; }
    friend self operator/(self x, value_type y) { return x /= y; }
    friend self operator%(self x, value_type y) { return x %= y; }
};

template<class Iter_T, int Step_N>
class kstride_iter
{
public:
    typedef typename std::iterator_traits<Iter_T>::value_type value_type;
    typedef typename std::iterator_traits<Iter_T>::reference reference;
    typedef typename std::iterator_traits<Iter_T>::difference_type difference_type;
    typedef typename std::iterator_traits<Iter_T>::pointer pointer;
    typedef std::random_access_iterator_tag iterator_category;
    typedef kstride_iter self;

    kstride_iter() : m(NULL) {}
    kstride_iter(const self& x) : m(x.m) {}
    explicit kstride_iter(Iter_T x) : m(x) {}
    explicit kstride_iter(Iter_T x, int step) : m(x) { Step_N = step; }

    self& operator++() { m += Step_N; return *this; }
    self operator++(int) { self tmp = *this; m += Step_N; return tmp; }
    self& operator+=(difference_type x) { m += x * Step_N; return *this; }
    self& operator--() { m -= Step_N; return *this; }
    self operator--(int) { self tmp = *this; m -= Step_N; return tmp; }
    self& operator-=(difference_type x) { m -= x * Step_N; return *this; }
    reference operator[](difference_type n) { return m[n*Step_N]; }
    reference operator*() { return *m; }

    friend bool operator==(self x, self y) { return x.m == y.m; }
    friend bool operator!=(self x, self y) { return x.m != y.m; }
    friend bool operator<(self x, self y) { return x.m < y.m; }
    friend difference_type operator-(self x, self y) { return (x.m - y.m) / Step_N; }
    friend self operator+(self x, difference_type y) { return x += y * Step_N; }
    friend self operator+(difference_type x, self y) { return y += x * Step_N; }
private:
    Iter_T m;
};

template<class Value_T, int Rows_N, int Cols_N>
class kmatrix
{
public:
    typedef Value_T value_type;
    typedef kmatrix self;
    typedef value_type* iterator;
    typedef const value_type* const_iterator;
    typedef kstride_iter<Value_T*, 1> row_type;
    typedef kstride_iter<value_type*, Cols_N> col_type;
    typedef kstride_iter<const Value_T*, 1> const_row_type;
    typedef kstride_iter<const value_type*, Cols_N> const_col_type;

    static const int nRows = Rows_N;
    static const int nCols = Cols_N;

    kmatrix()  { m = Value_T(); }
    kmatrix(const self& x)  { m = x.m; }
    explicit kmatrix(Value_T& x) { m = x.m; }

    static int rows() { return Rows_N; }
    static int cols() { return Cols_N; }
    static int size() { return Rows_N * Cols_N; }

    row_type row(int n) { return row_type(begin() + (n * Cols_N)); }
    col_type col(int n) { return col_type(begin() + n); }
    const_row_type row(int n) const { return const_row_type(begin() + (n * Cols_N)); }
    const_col_type col(int n) const { return const_col_type(begin() + n);  }
    iterator begin() { return m.begin(); }
    iterator end() { return m.begin() + size(); }
    const_iterator begin() const { return m; }
    const_iterator end() const { return m + size(); }

    self& operator=(const self& x) { m = x.m; return *this; }
    self& operator=(value_type x) { m = x; return *this; }
    row_type operator[] (int n) { return row(n); }
    const_row_type operator[] (int n) const { return row(n); }
    self& operator+=(const self& x) { m += x.m; return *this; }
    self& operator-=(const self& x) { m -= x.m; return *this; }
    self& operator+=(value_type x) { m += x; return *this; }
    self& operator-=(value_type x) { m -= x; return *this; }
    self& operator*=(value_type x) { m *= x; return *this; }
    self& operator/=(value_type x) { m /= x; return *this; }
    self operator-() { return -m; }

    friend self operator+(self x, const self& y) { return x += y; }
    friend self operator-(self x, const self& y) { return x -= y; }
    friend self operator+(self x, value_type y) { return x += y;  }
    friend self operator-(self x, value_type y) { return x -= y;  }
    friend self operator*(self x, value_type y) { return x *= y;  }
    friend self operator/(self x, value_type y) { return x /= y;  }
    friend bool operator==(const self& x, const self& y) { return x != y; }
    friend bool operator!=(const self& x, const self& y) { return x.m != y.m; }
private:
    kvector<Value_T, (Rows_N + 1) * Cols_N> m;
};

template<class Iter_T>
void outputRowOrColum(Iter_T iter, int n)
{
    for (int i = 0; i < n; ++i)
        cout << iter[i] << " ";
    cout << endl;
}

template<class Matrix_T>
void initializeMatrix(Matrix_T& m)
{
    int k = 0;
    for (int i = 0; i < m.rows(); ++i)
        for (int j = 0; j < m.cols(); ++j)
            m[i][j] = k++;
}

template<class Matrix_T>
void outputMatrix(Matrix_T& m)
{
    for (int i = 0; i < m.rows(); ++i)
    {
        cout << "Row " << i << " = ";
        outputRowOrColum(m.row(i), m.cols());
    }
    for (int i = 0; i < m.cols(); ++i)
    {
        cout << "Column " << i << " = ";
        outputRowOrColum(m.col(i), m.rows());
    }
}

int main(int argc, char** argv)
{
    kmatrix<int , 2, 4> m;
    initializeMatrix(m);
    m *= 2;
    outputMatrix(m);
    system("pause");
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值