您的位置:首页 > 其它

矩阵模板,右元函数重载<<操作符

2015-04-07 13:58 225 查看
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;
    }
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: