注意点:
1.const的作用和基本运算符重载:
C++几种运算符重载: +,-,+=,==,!=,++ ,负号,<<,>>
2.等号运算符两边必须是已创建的对象:
C++等号运算符重载
矩阵的基本操作
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <algorithm>
using namespace std;
int rn[100005], rs[100005];
class Triple
{
public:
int x, y, v;
Triple()
{
x = y = v = 0;
}
Triple(int x_, int y_, int v_)
{
x = x_;
y = y_;
v = v_;
}
Triple& operator= (const Triple &t)
{
this->x = t.x;
this->y = t.y;
this->v = t.v;
return *this;
}
bool operator< (const Triple &t)//先按行比较 后按列
{
if (x < t.x || (x == t.x && y < t.y)) return true;
return false;
}
bool operator== (const Triple &t)//坐标相等
{
return (x == t.x && y == t.y);
}
};
class SqSMatrix
{
private:
Triple *arr;
int rows, cols, nums;
public:
SqSMatrix()
{
arr = NULL;
rows = cols = nums = 0;
}
SqSMatrix& operator= (const SqSMatrix &x)
{
if (this == &x) return *this;
this->Destroy();
this->arr = (Triple*)malloc(sizeof(Triple) * (x.nums+5));
for (int i = 0; i < x.nums; i++)
{
*(this->arr + i) = *(x.arr + i);
}
this->rows = x.rows;
this->cols = x.cols;
this->nums = x.nums;
return *this;
}
//重载变成成员函数 第一个const是防止返回值被赋值;第三个const是保证返回值又能调用operator+(),起到能连续+的作用
const SqSMatrix operator+(const SqSMatrix &b)const
{
SqSMatrix t;
t.arr = (Triple*)malloc(sizeof(Triple) * (nums + b.nums));
t.rows = rows;
t.cols = cols;
t.nums = 0;
int n = 0;
Triple *p = arr, *q = b.arr, *r = t.arr;
while (p - arr < nums || q - b.arr < b.nums)
{
if (q - b.arr >= b.nums || *p < *q)
{
*r = *p;
p++;
}
else if (p - arr > nums || *q < *p)
{
*r = *q;
q++;
}
else
{
r->x = p->x;
r->y = p->y;
r->v = p->v + q->v;
p++, q++;
}
r++;
n++;
}
t.nums = n;
return t;
}
const SqSMatrix operator-()const
{
SqSMatrix t;
t = *this;//两行不能写在一起
//等号运算符重载 不是构造函数,等号左右两边的对象必须已经被创建。
Triple *p = t.arr;
for (int i = 0; i < nums; i++)
{
p->v = -p->v;
p++;
}
return t;
}
const SqSMatrix operator-( SqSMatrix &b) const
{
return *this + (-b);
}
void Destroy();
void Create();
void Print();
SqSMatrix Transpose();
SqSMatrix FastTranspose();
};
void SqSMatrix::Destroy()
{
rows = cols = nums = 0;
free(arr);
}
void SqSMatrix::Create()
{
Destroy();
int n, r, c, x, y, v;
scanf("%d%d%d", &r, &c, &n);
rows = r;
cols = c;
nums = n;
arr = (Triple*)malloc(sizeof(Triple) * (n+5));
for (int i = 0; i < n; i++)
{
scanf("%d%d%d", &x, &y, &v);
*(arr+i) = Triple(x, y, v);
}
sort(arr, arr + n);//按行列排序
}
void SqSMatrix::Print()
{
Triple *p = arr;
for (int i = 0; i < rows; i++)
{
for (int j = 0; j < cols; j++)
if (p->x == i && p->y == j)
{
printf("%4d", p->v);
p++;
}
else printf("%4d", 0);
printf("\n");
}
printf("\n");
}
SqSMatrix SqSMatrix::Transpose()
{
SqSMatrix t;
t.rows = cols;
t.cols = rows;
t.nums = nums;
t.arr = (Triple*)malloc(sizeof(Triple) * nums);
if (nums > 0)
{
Triple *p = t.arr;
for (int i = 0; i < cols; i++)
for (int j = 0; j < nums; j++)
if ((arr+j)->y == i)
{
p->x = i;
p->y = (arr+j)->x;
p->v = (arr+j)->v;
p++;
}
}
return t;
}
SqSMatrix SqSMatrix::FastTranspose()
{
SqSMatrix t;
t.rows = cols;
t.cols = rows;
t.nums = nums;
t.arr = (Triple*)malloc(sizeof(Triple) * nums);
if (nums > 0)
{
for (int i = 0; i < cols; i++)
rn[i] = 0;
for (int i = 0; i <nums; i++)
rn[(arr+i)->y]++;
rs[0] = 0;
for (int i = 1; i < cols; i++)
rs[i] = rs[i - 1] + rn[i - 1];
Triple *p = arr;
for (int i = 0; i < nums; i++)
{
int q = rs[p->y];
*(t.arr + q) = Triple(p->y, p->x, p->v);
rs[p->y]++;
p++;
}
}
return t;
}
int main()
{
SqSMatrix a, b, c, d;
a.Create();
b.Create();
a.Print();
b.Print();
c = a + b;
c.Print();
c = -c;
c.Print();
c = a - c;
c.Print();
printf("trans\n");
d = a.Transpose();
d.Print();
d = c.FastTranspose();
d.Print();
return 0;
}
/*
6 10 8
0 0 3
3 2 9
5 9 100
5 3 6
1 9 32
4 0 14
5 7 84
0 4 21
6 10 5
0 0 7
5 9 22
4 9 31
5 2 8
2 5 21
*/