【问题标题】:What datatype would is expected for the arrays in c++?c++ 中的数组需要什么数据类型?
【发布时间】:2021-12-20 04:01:29
【问题描述】:

我一直在尝试为三维数组分配数据类型,例如,double,但一直收到错误

                   error: request for member 'size' in 'msd_x', which is of non-class 
                          type 'const sample_type' {aka 'const long unsigned int'}
                   -> size_t N = msd_x.size();
class mean_square_displacement
{
public:
    typedef boost::multi_array_types::size_type sample_type;
    //typedef fixed_vector<double, 3> result_type;
    typedef double result_type;

    result_type operator() (sample_type const& msd_x, sample_type const& msd_y, sample_type const& msd_z) const
    {
        size_t N = msd_x.size();
        result_type msd = 0;
        for (unsigned int i = 0; i < N; ++i) {
            for (unsigned int j = 0; j < N; ++j) {
                auto dr = (msd_x[i+1][j] - msd_x[i][j])  + (msd_y[i+1][j+1] - msd_y[i][j+1]) + (msd_z[i+1][j+2] - msd_z[i][j+2]); 
            }
            // accumulate squared displacements
            msd += dr * dr;
        }
        return msd / N;

    }
}

随之而来的是另一个错误:

                   error: invalid types 'const sample_type {aka const long  
                          unsigned int}[unsigned int]' for array subscript

我猜代码访问数组的方式很好。但它仍然会引发错误。读取的数据代码为double数据类型,如下代码所示:

int main(int argc, char const* argv[])
{
    correlator::multi_tau_correlator<double> corr(    // TODO replace sample type
        nsamples * sampling_interval / 30            trajectory length
      , sampling_interval                        // time resolution at lowest level
      , 10   
    );
    // define time correlation functions
    auto msd = make_correlation(correlator::mean_square_displacement(), corr);
    corr.add_correlation(msd);
    // main loop for all coordinates
    for (size_t i = 1; i < nsamples; ++i) {
        auto position_array = first_rows[i]; 
        // append data to the correlator, which possibly computes some time correlations
        corr.sample(position_array);
    }
    corr.finalise();

    return 0;
}

有人指出数据类型有什么问题吗?

【问题讨论】:

  • @TedLyngmo 同样的错误!
  • 错误:在'msd_x'中请求成员'size',它是非类类型'const sample_type' {aka 'const long unsigned int'} size_t N = msd_x.size;跨度>
  • 我不知道!!!
  • msd_xsample_type 类型,boost::multi_array_types::size_typeunsigned integral 类型。所以它只是一些 unsigned int 类型,它是原始类型,而不是对象。因此,它没有成员函数或变量。
  • 这也是错误告诉你的:msd_x 的类型是 const long unsigned int

标签: c++ boost


【解决方案1】:

我根据导致此的许多问题在这里猜测。

很明显,您不希望样本类型是标量,而是二维数组。

我将绘制一个通用快捷方式,即使您不知道参数的具体类型,您也可以编写 mean_square_displacement::operator() 来接受这些。

注意

但是,我首先要提醒的是,没有办法知道这是否适用于您从未描述过的 multi_tau_correlator 框架。您似乎正在尝试以并行方式应用相关器。

首先,不知道 correlator 框架是否允许您这样做(技术上),其次它可能不会很快,因为这种并行性是高度优化库的领域(使用实际的矢量化操作,如SIMD、AVX、OpenGL/Cuda)。

我在这里看到的只是一个原始的二次循环,这对效率来说并不是一个好兆头。

盲目修复

既然您不希望 sample_type 成为您定义的那样,只需将其定义为其他东西,最好是您需要的东西!

typedef array_2d_t sample_type;

很有可能这也不是您实际传递的内容(我怀疑是 sub_array 视图或 3D 多数组的(可选跨步)multi_array_view 切片)。但是你可以让编译器根据你知道你可以像arg[int][int]这样索引参数这一事实来计算它:

template <typename Sample>
result_type operator()(Sample const& msd_x, Sample const& msd_y,
                       Sample const& msd_z) const

这让编译器推断出具体类型,假设所有参数都具有相同的静态类型。否则,您可以更加开放:

template <typename Sx, typename Sy, typename Sz>
result_type operator()(Sx const& msd_x, Sy const& msd_y,
                       Sz const& msd_z) const

更多注意

我可以看出你已经超出了你的深度。例如

  • 在您的mean_square_displacement 中,我们看到auto dr 在其范围之外使用。它不会编译。对于上述内容,我猜测您打算将累积放入内部循环中。
  • 您还明确msd_x 越界(索引N 不存在,因为N == msd_x.size())。
  • msd_y/msd_z 可能具有相同的范围,所以他们也有这个问题,但即使是 +2
  • 如果天真的修复循环到N-2 再次冒险UB 除非您确保N 永远不会

以下是使此代码看起来在技术上可行的最低限度的修复:

struct mean_square_displacement {
    //typedef fixed_vector<double, 3> result_type;
    typedef double result_type;

    template <typename Sample>
    result_type operator()(Sample const& msd_x, Sample const& msd_y,
                           Sample const& msd_z) const
    {
        size_t N = msd_x.size();
        assert(N>=2);
        result_type msd = 0;
        for (unsigned int i = 0; i < N-1; ++i) {
            for (unsigned int j = 0; j < N-2; ++j) {
                auto dr = //
                    (msd_x[i + 1][j + 0] - msd_x[i][j + 0]) +
                    (msd_y[i + 1][j + 1] - msd_y[i][j + 1]) +
                    (msd_z[i + 1][j + 2] - msd_z[i][j + 2]);
                // accumulate squared displacements
                msd += dr * dr;
            }
        }
        return msd / N;

    }
};

【讨论】:

猜你喜欢
  • 2021-05-01
  • 2015-02-07
  • 2015-10-23
  • 2012-04-05
  • 2011-08-29
  • 1970-01-01
  • 2015-08-26
  • 2021-06-09
  • 1970-01-01
相关资源
最近更新 更多