如果模板类型是模板本身,如何获取迭代器?

时间:2010-12-13 14:54:59

标签: c++ templates boost sparse-matrix

我的第一个模板类尝试从一些boost compressed_matrix到PETSc Sparse AIJ格式的简化转换器,但我在一开始就遇到迭代器typedef declerations的问题。所以T是compressed_matrix,天真地使用下面的代码会导致编译时错误,应该如何定义这些迭代器。我应该多阅读一下这个模板......

template <class T, class T1>
int converter<T, T1>::convertMBo2Pe( const T& A,
                           T1 A_ ){
    PetscErrorCode ierr;
    int cntNnz = 0;
    typedef T::iterator1 i1_t;
    typedef T::iterator2 i2_t;
    //int nnz[ooFelieMatrix.size1()];
    int nnz[A.size1()];
    unsigned ind=0; 
    //get information about the matrix

    double* vals = NULL;
    for (i1_t i1 = A.begin1(); i1 != A.end1(); ++i1)
    {
      nnz[ind] = distance(i1.begin(), i1.end()); 
      ind++; 
    }
    // create the matrix depending 
    // on the values of the nonzeros
    // on each row
    ierr = MatCreateSeqAIJ( PETSC_COMM_SELF, A.size1(), 
                                                  A.size2(), cntNnz, nnz, 
                                                  A_ );
    PetscInt rInd = 0, cInd=0; 
    PetscInt* rCount, dummy; 
    rCount = &dummy;
    // pointer to values in a row
    PetscScalar*   valsOfRowI = NULL;
    PetscInt*  colIndexOfRowI = NULL;
    PetscInt rC = 1;
    for(i1_t i1 = A.begin1(); i1 != A.end1(); ++i1)
    {
      // allocate space for the values of row I
      valsOfRowI     = new PetscScalar[nnz[rInd]];
      colIndexOfRowI = new PetscInt[nnz[rInd]];
      for(i2_t i2 = i1.begin(); i2 != i1.end(); ++i2)
      {
         colIndexOfRowI[cInd] = i2.index2();
         valsOfRowI[cInd]     = *i2;
         cInd++;
      }
      // setting one row each time
      *rCount = rInd;
      MatSetValues( A_, rC, rCount, nnz[rInd], 
                                  colIndexOfRowI, valsOfRowI,
                  INSERT_VALUES );
      // delete
      delete [] valsOfRowI;
      delete [] colIndexOfRowI;
      rInd++; cInd = 0;
    }
    //
    MatAssemblyBegin( A_, MAT_FINAL_ASSEMBLY );
    MatAssemblyEnd( A_, MAT_FINAL_ASSEMBLY );
    // return 
    return 0;   
}

1 个答案:

答案 0 :(得分:1)

您必须明确告诉编译器T::iterator1是一个类型名称(与静态变量相反,或者沿着这些行)。

typedef typename T::iterator1 i1_t;

相关问题