![](/img/trans.png)
[英]How do you create a dynamic array for struct elements of different variable types for c++?
[英]How to create a C++ array with variable elements?
我有这个C ++程序:
#include <iostream>
#include <vector>
#include <string>
#include <fstream>
#include <cmath>
using namespace std;
double dx2(int t, int x, int dx)
{
return (-9.8*cos(x));
}
int square(int x)
{
return (x*x);
}
double RK4(float t, float x, float dx, float h)
{
double k1, k2, k3, k4, l1, l2, l3, l4, diff1, diff2;
k1 = h*dx2(t,x,dx);
l1 = h*k1;
k2 = h*dx2(t+h/2,x+l1/2,dx+k1/2);
l2 = h*k2;
k3 = h*dx2(t+h/2,x+l2/2,dx+k2/2);
l3 = h*k3;
k4 = h*dx2(t+h,x+l3,dx+k3);
l4 = h*k4;
diff1 = (l1+2*l2+2*l3+l4)/float(6);
diff2 = (k1+2*k2+2*k3+k4)/float(6);
double OUT[] = {diff1, diff2};
return OUT;
}
int main()
{
double diff, t, t0, t1, x, x0, dx, dx0, h, N;
N = 1000;
t0 = 0;
t = t0;
t1 = 10;
x0 = 0;
x = x0;
dx0 = 0;
dx = dx0;
h = (t1 - t0) / float(N);
for(int i = 1; i<=N; i++) {
diff = RK4(t,x,dx,h);
x = x + diff;
t = t + h;
}
cout << diff;
return 0;
}
正如您在该程序中看到的那样,我正在求解二阶微分方程(如果可以将LaTeX方程插入我的问题中,请告诉我):
d2x / dt2 = -9.8 cos(x)
这是简单摆运动方程的一个例子。 问题行是33和34。我试图将OUT数组的第一个元素定义为diff1,将第二个元素定义为diff2。 每当我编译该程序(名为example.cpp
)时,都会出现错误:
g++ -Wall -o "example" "example.cpp" (in directory: /home/fusion809/Documents/CodeLite/firstExample)
example.cpp: In function ‘double RK4(float, float, float, float)’:
example.cpp:33:9: error: cannot convert ‘double*’ to ‘double’ in return
return OUT;
^~~
Compilation failed.
确实,由于您要返回一个double
数组,因此衰减为double*
,但是该函数定义为返回double
。 类型的数组T
和类型T
是不同类型的在C ++中,并且它们不能被转换之间,一般而言。
在这种情况下,最好使用std::pair<T1, T2>
( #include <utility>
),因为您使用的是C ++和标准库,或者具有两个类型为double
字段的结构。 查找std::pair<>
和std::tie<>
,前者用于制作成对的不同类型的元素,而后者用于制作成任意大小的不同类型的元组。
当您将std::pair
的元素写入std::cout
,请使用first
, second
成员访问该对的字段。 无法使用std::cout
的重载流运算符直接输出std::pair
。
编辑:
#include <utility>
std::pair<double, double> RK4(float t, float x, float dx, float h)
{
/* snip */
diff1 = (l1+2*l2+2*l3+l4)/float(6);
diff2 = (k1+2*k2+2*k3+k4)/float(6);
return {diff1, diff2};
}
int main()
{
double x, dx;
/* snip */
for(int i = 1; i<=N; i++) {
std::pair<double, double> diff = RK4(t,x,dx,h);
// or use with C++11 and above for brevity
auto diff = RK4(t,x,dx,h);
x = x + diff.first;
dx = dx + diff.second;
t = t + h;
}
cout << x << " " << dx << "\n" ;
return 0;
}
RK4
函数的返回类型为double
,这是一个单一值,但是您试图返回其中两个的数组。 那行不通。 您可以将返回类型更改为double*
并使用new double[2]
分配数组,但是使用std::pair<double, double>
作为返回类型会更简单安全。 然后就可以return { diff1, diff2 };
。
要从函数返回多个值,您可以有多种选择:
由于您返回的所有类型都是相同的,因此您可以返回数组:
std::array<double, 2> RK4(float t, float x, float dx, float h) { // ... return {{diff1, diff2}}; }
或std::vector
std::vector<double> RK4(float t, float x, float dx, float h) { // ... return {{diff1, diff2}}; }
您可以返回std::tuple
或std::pair
(限制为2个元素):
std::pair<double, double> RK4(float t, float x, float dx, float h) { // ... return {{diff1, diff2}}; }
要么
std::tuple<double, double> RK4(float t, float x, float dx, float h) { // ... return {{diff1, diff2}}; }
您也可以创建一个自定义类
struct RK4Result { double diff1; double diff2; }; RK4Result RK4(float t, float x, float dx, float h) { // ... return {diff1, diff2}; }
对于要移动的昂贵类型,您可以使用任何以前的方法,但是要使用out参数:
struct RK4Result { double diff1; double diff2; }; void RK4(float t, float x, float dx, float h, RK4Result& res) { // ... res = {diff1, diff2}; }
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.