• <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
    <noscript id="pjuwb"></noscript>
          <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
            <dd id="pjuwb"></dd>
            <abbr id="pjuwb"></abbr>

            稀疏矩陣的存取

            // test21.cpp : Defines the entry point for the console application.
            //

            #include 
            "stdafx.h"
            #include
            <iostream>
            #include
            <vector>
            #include
            <iomanip>
            using namespace std;

            template
            <class T>
            class Matrix;

            template 
            <class T>
            class Term{//矩陣中每個三元組的元素
            public:
                Term(
            int r,int c,T v):row(r),col(c),val(v){}
            //private:
                int row, col;//元素的行標(biāo),列標(biāo)
                T val;//元素的值
                friend class Matrix<T>;
            };

            template 
            <class T>
            class Matrix{//矩陣
                template<class T>
                friend istream
            & operator>>(istream&, Matrix<T>&);
                template
            <class T>
                friend ostream
            & operator<<(ostream&, Matrix<T>&);
            public:
                Matrix(){mRow
            =0, mCol=0, nZeroNum=0;}
            private:
                vector
            <Term<T>> vec;
                
            int mRow,mCol;//矩陣的行數(shù),列數(shù)
                int nZeroNum;//矩陣中的非零元素個數(shù)
            };

            template
            <class T>
            istream
            & operator>>(istream& in, Matrix<T>& m){//輸入矩陣
                cout<<"Enter the Row and Col of the Matrix: ";
                
            in>>m.mRow>>m.mCol;    //輸入矩陣的行數(shù)和列數(shù)
                cout<<"Enter the none Zeor Number of the Matrix: ";
                
            in>>m.nZeroNum;//輸入矩陣的非零元個數(shù)
                int r,c;
                T v;
                
            int k=m.nZeroNum;
                
            while(k--!=0){
                    cout
            <<"Enter the Elements of the Matrix: ";
                    
            in>>r>>c>>v;//矩陣中每一個非零元的行標(biāo),列標(biāo)和值
                    m.vec.push_back(Term<T>(r,c,v));//將元素存到容器中去
                }
                
            return in;
            }

            template
            <class T>
            ostream
            & operator<<(ostream& out, Matrix<T>& m){//輸出矩陣
                
            //不用STL迭代器,手寫如下
                
            //int k=0;//用于遍歷vec中的每一個值
                
            //for(int i=0;i<m.mRow;i++){
                
            //    for(int j=0;j<m.mCol;j++){
                
            //        if(m.vec[k].row==i && m.vec[k].col==j)//如果m中vec中的第k個非零元素對應(yīng)行標(biāo)=i,列標(biāo)=j,則輸出該值
                
            //            out<<setw(4)<<m.vec[k++].val;
                
            //        else
                
            //            out<<setw(4)<<"0";    //如果不存在第k個元素,輸出0
                
            //    }
                
            //    out<<endl;
                
            //}
                vector<Term<T>>::iterator iter=m.vec.begin(); 
                
            while(iter!=m.vec.end()){
                    
            for(int i=0;i<m.mRow;i++){
                        
            for(int j=0;j<m.mCol;j++){
                            
            if(iter->row==&& iter->col==j){
                                
            out<<setw(4)<<iter->val;//如果存在非零元素,則打印
                                iter++;
                            }
                            
            else
                                
            out<<setw(4)<<"0";//否則打印0                
                        }
                        
            out<<endl;
                    }
                }
                
            return out;
            }

            int main(){
                Matrix
            <int> m;
                cin
            >>m;
                cout
            <<m;
                system(
            "pause");
            }



            運行結(jié)果如下:
            Enter the Row and Col of the Matrix: 3 3
            Enter the none Zeor Number of the Matrix: 3
            Enter the Elements of the Matrix: 0 0 1
            Enter the Elements of the Matrix: 1 1 2
            Enter the Elements of the Matrix: 2 2 3
               1   0   0
               0   2   0
               0   0   3

            posted on 2008-10-26 16:41 deep2 閱讀(1548) 評論(0)  編輯 收藏 引用 所屬分類: 矩陣


            只有注冊用戶登錄后才能發(fā)表評論。
            網(wǎng)站導(dǎo)航: 博客園   IT新聞   BlogJava   博問   Chat2DB   管理


            <2008年10月>
            2829301234
            567891011
            12131415161718
            19202122232425
            2627282930311
            2345678

            導(dǎo)航

            統(tǒng)計

            常用鏈接

            留言簿(1)

            隨筆分類

            隨筆檔案

            搜索

            最新評論

            閱讀排行榜

            評論排行榜

            久久亚洲sm情趣捆绑调教| 久久综合一区二区无码| 亚洲精品乱码久久久久久按摩 | 久久国产精品久久| 精品久久久无码中文字幕| 国产精品久久新婚兰兰| 久久精品蜜芽亚洲国产AV| 99久久国产综合精品网成人影院| 久久伊人色| 国产一级持黄大片99久久| 久久精品国产欧美日韩99热| 成人免费网站久久久| 久久99热这里只频精品6| 99久久免费国产精精品| 亚洲午夜久久久久久久久电影网| 91亚洲国产成人久久精品网址| 久久婷婷五月综合97色直播| 国产精品嫩草影院久久| 丰满少妇高潮惨叫久久久| 久久久午夜精品福利内容| 99久久精品国产一区二区蜜芽| 亚洲AV日韩精品久久久久| 亚洲午夜久久久| 久久强奷乱码老熟女| 51久久夜色精品国产| 狠狠色丁香久久婷婷综合五月| 久久久SS麻豆欧美国产日韩| 日批日出水久久亚洲精品tv| 国产成人综合久久久久久| 97久久精品人妻人人搡人人玩| 18岁日韩内射颜射午夜久久成人| 久久人人青草97香蕉| 久久久久亚洲精品无码网址| 色综合久久88色综合天天| 狠色狠色狠狠色综合久久| 2022年国产精品久久久久| 久久久久成人精品无码中文字幕 | 亚洲狠狠婷婷综合久久蜜芽| 伊人伊成久久人综合网777| 香蕉99久久国产综合精品宅男自 | 亚洲精品蜜桃久久久久久|