• <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;//元素的行標,列標
                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;//矩陣的行數,列數
                int nZeroNum;//矩陣中的非零元素個數
            };

            template
            <class T>
            istream
            & operator>>(istream& in, Matrix<T>& m){//輸入矩陣
                cout<<"Enter the Row and Col of the Matrix: ";
                
            in>>m.mRow>>m.mCol;    //輸入矩陣的行數和列數
                cout<<"Enter the none Zeor Number of the Matrix: ";
                
            in>>m.nZeroNum;//輸入矩陣的非零元個數
                int r,c;
                T v;
                
            int k=m.nZeroNum;
                
            while(k--!=0){
                    cout
            <<"Enter the Elements of the Matrix: ";
                    
            in>>r>>c>>v;//矩陣中每一個非零元的行標,列標和值
                    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個非零元素對應行標=i,列標=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");
            }



            運行結果如下:
            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 閱讀(1559) 評論(0)  編輯 收藏 引用 所屬分類: 矩陣

            <2025年8月>
            272829303112
            3456789
            10111213141516
            17181920212223
            24252627282930
            31123456

            導航

            統計

            常用鏈接

            留言簿(1)

            隨筆分類

            隨筆檔案

            搜索

            最新評論

            閱讀排行榜

            評論排行榜

            久久激情五月丁香伊人| 99久久国产宗和精品1上映 | 久久婷婷五月综合色99啪ak| 伊人色综合久久天天网| 韩国三级大全久久网站| 亚洲午夜精品久久久久久app| 国产精品美女久久久久久2018| 久久久久国产精品麻豆AR影院| 色8久久人人97超碰香蕉987| 久久精品国产国产精品四凭 | 久久AV高潮AV无码AV| 国内精品久久久久久久久电影网| 婷婷久久香蕉五月综合加勒比| 国产亚洲成人久久| 一本大道加勒比久久综合| 日韩精品久久无码中文字幕| 久久综合九色综合久99| 精品视频久久久久| 中文字幕亚洲综合久久2| 97久久精品无码一区二区| 伊人久久大香线蕉AV色婷婷色 | 久久夜色精品国产噜噜噜亚洲AV| 亚洲一级Av无码毛片久久精品| 久久国产热这里只有精品| 中文字幕久久欲求不满| 伊人久久大香线蕉影院95| 精品久久久久香蕉网| 国产产无码乱码精品久久鸭 | 欧美性大战久久久久久| 狠狠人妻久久久久久综合蜜桃| 色综合久久中文色婷婷| 久久综合狠狠综合久久激情 | 久久成人国产精品二三区| 97精品久久天干天天天按摩| 91精品国产高清91久久久久久| 久久发布国产伦子伦精品| 国产国产成人精品久久| 色综合久久中文色婷婷| 久久精品中文字幕有码| 亚洲国产日韩欧美久久| 亚洲av伊人久久综合密臀性色|