C++ 如何将Boost multi_数组传递给函数?
我正试图使我用boost创建的数组与函数一起工作。我写了下面的代码,但它不起作用。 如何使数组由函数读取?在这段代码的前一个版本中,我刚刚定义了类似双U[N+1][4]的数组。它成功了。使用boost时我做错了什么C++ 如何将Boost multi_数组传递给函数?,c++,boost,C++,Boost,我正试图使我用boost创建的数组与函数一起工作。我写了下面的代码,但它不起作用。 如何使数组由函数读取?在这段代码的前一个版本中,我刚刚定义了类似双U[N+1][4]的数组。它成功了。使用boost时我做错了什么 #include "boost/multi_array.hpp" #include <cassert> #include <iostream> #include <cmath> #include <time.h> int const
#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;
}
#包括“boost/multi_array.hpp”
#包括
#包括
#包括
#包括
int const N=400;
int常数x1=0;
int const x2=1;
int const gammaValue=1.4;
无效输出(双U[N][3])
{
双x,dx;
dx=(双(x2-x1))/double(N);
文件*fp;
双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”);
对于(int n=0;n使用的成员函数data()
,它返回一个指针,指向包含数组数据的连续块的开头。获得第一个元素的地址后,可以获得其他元素,因为您已经知道数组的维度
double * p = A.data();
double (*array)[3] = (double (*)[3])p;
Output(array);
使用的成员函数data()
,它将返回一个指针,指向包含数组数据的连续块的开头。一旦获得第一个元素的地址,就可以获得其他元素,因为您已经知道数组的维度
double * p = A.data();
double (*array)[3] = (double (*)[3])p;
Output(array);
使用的成员函数data()
,它将返回一个指针,指向包含数组数据的连续块的开头。一旦获得第一个元素的地址,就可以获得其他元素,因为您已经知道数组的维度
double * p = A.data();
double (*array)[3] = (double (*)[3])p;
Output(array);
使用的成员函数data()
,它将返回一个指针,指向包含数组数据的连续块的开头。一旦获得第一个元素的地址,就可以获得其他元素,因为您已经知道数组的维度
double * p = A.data();
double (*array)[3] = (double (*)[3])p;
Output(array);
为什么不直接更改输出函数的签名?将数组类型
typedef移到文件顶部,然后将输出函数更改为:
void输出(数组类型&U)
或者,最好将参数设置为const
。下面是一些代码:
#include <boost/multi_array.hpp>
#include <iostream>
typedef boost::multi_array< double, 2 > array_type;
void Output( const array_type& arr )
{
std::cout << "here" << std::endl;
}
int main( int argc, char ** argv )
{
array_type arr;
Output( arr );
return 0;
}
#包括
#包括
typedef boost::多数组数组类型;
无效输出(常量数组类型和arr)
{
std::cout为什么不更改输出函数的签名?将array_type
typedef移到文件顶部,然后将输出函数更改为:
void输出(数组类型&U)
或者,最好将参数设置为const
。下面是一些代码:
#include <boost/multi_array.hpp>
#include <iostream>
typedef boost::multi_array< double, 2 > array_type;
void Output( const array_type& arr )
{
std::cout << "here" << std::endl;
}
int main( int argc, char ** argv )
{
array_type arr;
Output( arr );
return 0;
}
#包括
#包括
typedef boost::多数组数组类型;
无效输出(常量数组类型和arr)
{
std::cout为什么不更改输出函数的签名?将array_type
typedef移到文件顶部,然后将输出函数更改为:
void输出(数组类型&U)
或者,最好将参数设置为const
。下面是一些代码:
#include <boost/multi_array.hpp>
#include <iostream>
typedef boost::multi_array< double, 2 > array_type;
void Output( const array_type& arr )
{
std::cout << "here" << std::endl;
}
int main( int argc, char ** argv )
{
array_type arr;
Output( arr );
return 0;
}
#包括
#包括
typedef boost::多数组数组类型;
无效输出(常量数组类型和arr)