C++ 将数组中浮点的指针传递给接受固定大小数组的函数

C++ 将数组中浮点的指针传递给接受固定大小数组的函数,c++,arrays,pointers,parameter-passing,C++,Arrays,Pointers,Parameter Passing,我有许多函数,其形式如下: typedef float arr3[3]; float newDistanceToLine(arr3 &p0, arr3 &p1, arr3 &p2); 现在可以方便地将大量点存储到长阵列中: int n_points = 14; float *points; points = new float[3*n_points]; 是否有方法将指向数组“点”不同值的指针传递给接受固定大小数组的函数?我知道以下操作失败,但我想做如下操作: newD

我有许多函数,其形式如下:

typedef float arr3[3];
float newDistanceToLine(arr3 &p0, arr3 &p1, arr3 &p2);
现在可以方便地将大量点存储到长阵列中:

int n_points = 14;
float *points;
points = new float[3*n_points];
是否有方法将指向数组“点”不同值的指针传递给接受固定大小数组的函数?我知道以下操作失败,但我想做如下操作:

newDistanceToLine(&points[3], &points[6], &points[9]);
或者获得关于如何最好地重用我的代码的任何帮助


谢谢

更改
newDistanceToLine
的界面,以使用基于模式的类型,该模式可以被称为
array\u View
span
-阅读此内容

大概是这样的:

typedef float arr3[3];
class arr3_view
{
public:
    arr3_view(arr3& arr) : data(arr) {}
    arr3_view(float* data, std::size_t size) : data(data) 
    {
        if (size != 3) // or < 3 - I am not sure what is better for your case
          throw std::runtime_error("arr3 - wrong size of data: " + std::to_string(size));
    }

    float* begin() { return data; }
    float* end() { return data + 3; }
    float& operator [](std::size_t i) { return data[i]; }
    // and similar stuff as above for const versions

private:
    float* data;
};

float newDistanceToLine(arr3_view p0, arr3_view p1, arr3_view p2);

改用数据结构

struct SPosition
{
SPosition( float x = 0, float y = 0, float z = 0)
    :X(x)
    ,Y(y)
    ,Z(z)
{

}
float X;
float Y;
float Z;
};

std::vector<SPosition> m_positions;

float newDistanceToLine( const SPosition& pt1, const SPosition& pt2, const SPosition& pt3 )
{
    // to do
    return 0.f;
};
struct-SPosition
{
位置(浮动x=0,浮动y=0,浮动z=0)
:X(X)
,Y(Y)
,Z(Z)
{
}
浮动X;
浮动Y;
浮动Z;
};
std::向量m_位置;
浮动新距离线(常数位置和pt1、常数位置和pt2、常数位置和pt3)
{
//做
返回0.f;
};

使用标准库
std::vector
可爱的答案,PiotrNycz。我会尝试类似的东西。就性能而言,您认为值得尝试C++11“span”或任何其他替代方案吗?span不应该有任何性能损失-它只是我介绍的通用(模板)版本。但我想这不是C++11,而是一些编译器/库扩展-请看这个答案:但是如果您的环境中有这个
span
,那么就使用它吧…好吧,我刚刚编写了您的方法,将
arr3&arr
更改为
arr3(&arr)
使它更灵活,并对它进行了模板化。它工作得很好!!
struct SPosition
{
SPosition( float x = 0, float y = 0, float z = 0)
    :X(x)
    ,Y(y)
    ,Z(z)
{

}
float X;
float Y;
float Z;
};

std::vector<SPosition> m_positions;

float newDistanceToLine( const SPosition& pt1, const SPosition& pt2, const SPosition& pt3 )
{
    // to do
    return 0.f;
};