1.矩阵元素类型,以及矩阵的宽高都声明为模板参数
2.双元操作符一般都重载为友元函数
cout << matrix :可以看成左操作符是输出流对象,右操作符时矩阵对象
string < string :它的友元函数声明为
friend bool operator > (const string& string1, const string& string2)
template <class T, unsigned int R, unsigned int C>
class MTVariable
{
public:
union {
T m[R][C];
};
inline unsigned int rsize(void) const
{
return R;
}
inline unsigned int csize(void) const
{
return C;
}
friend std::ostream& operator <<(std::ostream& os, const MTVariable<T, R, C>& v)
{
os << '[';
unsigned int c, r;
for (r = 0; r < R; ++r)
{
os.width(2);
for (c = 0; c < C-1; ++c)
{
os << v.m[r][c] << " ";
}
os << v.m[r][c] << ';';
}
os << ']';
return os;
}
class MTVariable
{
public:
union {
T m[R][C];
};
inline unsigned int rsize(void) const
{
return R;
}
inline unsigned int csize(void) const
{
return C;
}
friend std::ostream& operator <<(std::ostream& os, const MTVariable<T, R, C>& v)
{
os << '[';
unsigned int c, r;
for (r = 0; r < R; ++r)
{
os.width(2);
for (c = 0; c < C-1; ++c)
{
os << v.m[r][c] << " ";
}
os << v.m[r][c] << ';';
}
os << ']';
return os;
}