• <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{//矩陣中每個(gè)三元組的元素
            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;//矩陣中的非零元素個(gè)數(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;//輸入矩陣的非零元個(gè)數(shù)
                int r,c;
                T v;
                
            int k=m.nZeroNum;
                
            while(k--!=0){
                    cout
            <<"Enter the Elements of the Matrix: ";
                    
            in>>r>>c>>v;//矩陣中每一個(gè)非零元的行標(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迭代器,手寫(xiě)如下
                
            //int k=0;//用于遍歷vec中的每一個(gè)值
                
            //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個(gè)非零元素對(duì)應(yīng)行標(biāo)=i,列標(biāo)=j,則輸出該值
                
            //            out<<setw(4)<<m.vec[k++].val;
                
            //        else
                
            //            out<<setw(4)<<"0";    //如果不存在第k個(gè)元素,輸出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");
            }



            運(yùn)行結(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 閱讀(1550) 評(píng)論(0)  編輯 收藏 引用 所屬分類(lèi): 矩陣


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


            <2025年6月>
            25262728293031
            1234567
            891011121314
            15161718192021
            22232425262728
            293012345

            導(dǎo)航

            統(tǒng)計(jì)

            常用鏈接

            留言簿(1)

            隨筆分類(lèi)

            隨筆檔案

            搜索

            最新評(píng)論

            閱讀排行榜

            評(píng)論排行榜

            久久国产劲爆AV内射—百度| 久久精品国产99国产精品 | 伊人久久成人成综合网222| 狠狠人妻久久久久久综合| 久久久久久综合网天天| 久久亚洲AV成人无码国产| 国产成人99久久亚洲综合精品 | 久久夜色精品国产亚洲av| 久久久久久伊人高潮影院| 伊人色综合久久天天| 国产激情久久久久久熟女老人| 99久久免费国产特黄| 久久夜色精品国产噜噜亚洲a| 国产99久久精品一区二区| 久久亚洲精品无码播放| 国产精品一久久香蕉国产线看观看| 久久夜色精品国产亚洲av| 99久久成人18免费网站| 久久国产精品一国产精品金尊| 日韩中文久久| 久久影视综合亚洲| 国产免费福利体检区久久| 97久久国产亚洲精品超碰热| 亚洲午夜无码久久久久| 亚洲欧美日韩精品久久亚洲区| 成人精品一区二区久久久| 成人综合伊人五月婷久久| 国产美女亚洲精品久久久综合| 一本一道久久a久久精品综合 | 久久精品中文闷骚内射| 99久久精品免费看国产一区二区三区 | 少妇精品久久久一区二区三区| 一级a性色生活片久久无| 国产精品美女久久久免费| 国产99久久久国产精免费| 国产午夜电影久久| 久久国产福利免费| 无码精品久久一区二区三区| 亚洲国产香蕉人人爽成AV片久久 | 国产人久久人人人人爽| www.久久热|