使用地图容器创建矩阵 [英] Creating matrix using map container

查看:141
本文介绍了使用地图容器创建矩阵的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

   template < class A, class B, class C> class Matrix
  { 
    public:

   typedef map<B,C> M2; // for putting 1D info  
   typedef map<A,M2> M1;
   map<M1, M2 > data; 
   M1 m1;
   M2 m2;
   typedef  typename map<B,C>::iterator iterator2;
   iterator2 itr2; 
   typedef  typename map<A,M2>::iterator iterator1;
   iterator1 itr1;   
    Matrix()                             // default constructor
    { 
      m2.insert(pair<int,double> (1,10));
      m2.insert(pair<int,double> (2,20));
      m2.insert(pair<int,double> (3,30));
        m1.insert(pair <int, M2> (1,m2));
        m2.clear();

     m2.insert(pair<int,double> (1,40));
     m2.insert(pair<int,double> (2,50));
     m2.insert(pair<int,double> (3,60));
        m1.insert(pair <int, M2> (2,m2));
        m2.clear();

     m2.insert(pair<int,double> (1,70));
     m2.insert(pair<int,double> (2,80));
     m2.insert(pair<int,double> (3,90));
        m1.insert(pair <int, M2> (3,m2));
        m2.clear();                    }



    Matrix(const map<M1, M2>& ar)              //copy constructor 
    { m1 = ar.m1; }


    Matrix<A, B, C >& operator = (const Matrix<A, B, C >& ass)    //assignment
    { if (this==&ass) return *this;
      m1 = ass.m1; return *this; }


    const C& operator () ( int& index1,  int& index2 )  //for accessing/modifying data
     {    itr1 = m1.begin();
          advance(itr1,  index1 - 1);
          m2 = (*itr1).second;  
          itr2 = m2.begin();
          advance(itr2,  index2 - 1);             
          return (*itr2).second;      }
 };


int main()
{ 
  Matrix <int,int,double> mtr; 

   cout<< mtr(2,2);

    return 0;
  }

如果我编译上面的程序,我得到以下错误"cannot convert 'Matrix<int, int, double>' to 'double' in assignment." 我认为我尝试使运算符超载的方式存在问题,但到目前为止仍无法解决...

If I compile the above program I'm getting the following error "cannot convert 'Matrix<int, int, double>' to 'double' in assignment." I think there is a problem in the way I have tried to overload the operators but have been unable to solve it so far...

我知道这不是创建矩阵的有效方法,但是由于我是初学者,所以如果您能帮助我理解此代码背后的问题,它将很有帮助...

I know this isn't an efficient way of creating Matrices but since I'm a beginner ,it would help if you can help me understand the problem behind this code...

问题已解决! 谢谢

推荐答案

这可能无法解决您的所有问题,但这是一个悬而未决的问题:

This one may not resolve all your problems but it's an outstanding issue:

Matrix <int,int,double> mtr(); 

这行代码与您想的不一样.它没有定义对象mtr,而是声明了返回Matrix <int,int,double>类型的函数.

This line of code doesn't do what you thought. It doesn't define object mtr, instead it declares function which returns Matrix <int,int,double> type.

定义对象mtr

Matrix <int,int,double> mtr;

此外,地图迭代器不是random_access_iterator,您不能这样做

Also, map iterator is not random_access_iterator, you can't do

itr2 = m2.begin()+index2-1;

尝试:

itr2 = std::advance(m2.begin(),  index2 - 1);

这篇关于使用地图容器创建矩阵的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆