我想让我的数组创建与boost功能的工作。我写了下面的代码,但它不工作。 我该怎么做,我的数组被我的函数读取?在此代码的以前版本中,我只是将其定义为双U [N + 1] [4]。它的工作。我在使用boost时做错了什么?如何将Boost multi_array传递给函数?
#include "boost/multi_array.hpp"
#include <cassert>
#include <iostream>
#include <cmath>
#include <time.h>
int const N=400;
int const x1=0;
int const x2=1;
int const gammaValue=1.4;
void Output(double U[N][3])
{
double x,dx;
dx=(double(x2-x1))/double(N);
FILE *fp;
double rho,u,p;
fp=fopen("result.dat","w+");
fprintf(fp,"%2.30s\n %20.60s\n %20.18s\t %2.3d\t %2.18s\t ","TITLE = \"1D-EULER.dat \"","variables = \"x\", \"rho\", \"u\", \"p\"","zone i=",N+1,"f=point\n");
for(int n=0;n<N+1;n++)
{
x=x1+n*dx;
rho=U[n][0];
u=U[n][1]/U[n][0];
p=(gammaValue-1)*(U[n][2]-0.5*U[n][0]*u*u);
fprintf(fp,"%20.5f\t%20.5f\t%20.5f\t%20.5f\n",x,rho,u,p);
}
fclose(fp);
}
int main() {
// 3 x 4 x 2
typedef boost::multi_array<double, 2> array_type;
typedef array_type::index index;
array_type A(boost::extents[N][3]);
int values = 0;
for(index i = 0; i != N; ++i) {
for(index j = 0; j != 3; ++j){
A[i][j] = i+j;
}
}
Output(A);
return 0;
}
你的意思是输出(A.data()); –
不,你不能,'data()'只返回一个指针。 – jfly
那么你只需要一个转换,如我的答案所示。 – jfly