想写个稀疏矩阵,用三元组作为元素,编译错误一大堆,求教!~

napoleonpan 2008-10-25 09:31:21
三元组(x,y,v)分别存储矩阵中非零元的行标,列标和值

自己写的代码如下,编译起来一大堆错误。。。。求指正!!!!!!!!!!!!!!!!!

// 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 Term{//矩阵中每个三元组的元素
public:
Term(int r,int c,T v):row(r),col(c),val(v){}
private:
int row, col;//元素的行标,列标
T val;//元素的值
friend class Matrix;
};

template <class T>
class Matrix{//矩阵
friend istream& operator>>(istream&, Matrix&);
friend ostream& operator<<(ostream&, Matrix&);
public:
Matrix();
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;//矩阵中每一个非零元的行标,列标和值
vec.push_back(Term(r,c,v));//将元素存到容器中去
}
return in;
}

template<class T>
ostream& operator<<(ostream& out, Matrix<T>& m){//输出矩阵
vector<T>::iterator iter=m.vec.begin();
for(int i=0;i<m.mRow)
for(int j=0;j<m.mCol){
if(iter->mRow==i && iter->mCol==j)
out<<setw(4)<<iter->val;//如果存在非零元素,则打印
else
out<<setw(4)<<"0";//否则打印0
}
return out:
}

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

...全文
161 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
hityct1 2008-10-25
  • 打赏
  • 举报
回复
只是更改了基本的语法错误。使用vc9.0。

#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>;//少了<T>,Matrix是模板
};

template <class T>
class Matrix{//矩阵
template<class T>//加上
friend istream& operator>>(istream&, Matrix<T>&);//加上<T>,下同
template<class T>
friend ostream& operator<<(ostream&, Matrix<T>&);
public:
Matrix()//缺了构造函数的实现,最好将成员变量初始化
{};
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));//将元素存到容器中去 //加上m.
}
return in;
}

template<class T>
ostream& operator<<(ostream& out, Matrix<T>& m){//输出矩阵
//这个自己实现吧
/*vector<T>::iterator iter=m.vec.begin();
for(int i=0;i<m.mRow)
for(int j=0;j<m.mCol){
if(iter->mRow==i && iter->mCol==j)
out<<setw(4)<<iter->val;//如果存在非零元素,则打印
else
out<<setw(4)<<"0";//否则打印0
}*/
return out;
}

int main(){
Matrix<int> m;
cin>>m;
cout<<m;
system("pause");
}
liubuweiright 2008-10-25
  • 打赏
  • 举报
回复
顶一下,一分

有些语法没有见过,如://Matrix<int> m;
friend class Matrix; //之前好像没有声明Matrix;

33,008

社区成员

发帖
与我相关
我的任务
社区描述
数据结构与算法相关内容讨论专区
社区管理员
  • 数据结构与算法社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧