【问题标题】:How to pass the values stored in vector container to a new variable?如何将存储在向量容器中的值传递给新变量?
【发布时间】:2021-12-16 03:28:49
【问题描述】:

我想将向量中包含的值传递给一个新变量。但是好像类型转换有问题,因为反复抛出错误:

       error: cannot convert 'float*' to 'double' in 
              initialization
      error: cannot convert 'std::vector<float>' to 
             'double' in initialization

我尝试更改向量和变量的数据类型,但错误不断出现!

#include <boost/multi_array.hpp>
#include <h5xx/h5xx.hpp>
#include <iostream>
#include <vector>
#include <algorithm>

using array_2d_t = boost::multi_array<float, 2>;

h5xx::dataset open_dataset(std::string const& filename) {
    h5xx::file xaa(filename, h5xx::file::mode::in);
    h5xx::group g(xaa, "particles/lipids/box/positions");
    return h5xx::dataset(g, "value");
}

std::vector<float> cell_from_all_frames(h5xx::dataset& ds, size_t row, size_t col) {
    // determine dataset shape: frames, particle count, space dimension
    auto ds_shape = h5xx::dataspace(ds).extents<3>();
    std::vector<float> cells(ds_shape[0]); // number of frames

    std::vector<hsize_t> offsets{0, row, col};
    std::vector<hsize_t> counts{ds_shape[0], 1, 1};
    h5xx::slice slice(offsets, counts);

    h5xx::read_dataset(ds, cells, slice);
    return cells;
}

int main(int argc, char const* argv[])
{
    if (argc < 2) {
        std::cout << "Usage: " << argv[0] << " input.h5" << std::endl;
        return -1;
    }

    auto ds = open_dataset(argv[1]);
    std::vector<float> first_cells = cell_from_all_frames(ds, 0, 0);
    size_t nsamples = first_cells.size();
    std::cout << "no. of samples: " << nsamples ;
    double sampling_interval = 1;       // time between samples

    correlator::multi_tau_correlator<double> corr(    // TODO replace sample type
        nsamples * sampling_interval / 30        // max lag time: fraction of total trajectory length
      , sampling_interval                        // time resolution at lowest level
      , 10                                       // block size   // FIXME pass as (optional) command line argument
    );

    // define time correlation functions
    auto msd = make_correlation(correlator::mean_square_displacement(), corr);
    corr.add_correlation(msd);

    // main loop
    for (size_t i = 1; i < nsamples; ++i) {
        double position_array = first_cells();    
        //double position_array = static_cast<double>(std::rand()) / RAND_MAX;
        std::cout << "position arrays: " << position_array << std::endl;
        // append data to the correlator, which possibly computes some time correlations
        corr.sample(position_array);
    }
    corr.finalise();

    return 0;
}

问题在于注释主循环下的 main() 函数。我想将存储在 first_cells 中的值传递给 position_array,但它会引发上述错误。我尝试传递一些随机数并猜猜是什么,它工作正常!

【问题讨论】:

  • double position_array 不是一个数组,它只是一个数字。不知道你想要什么,请添加corr::sample的声明并请删除95%与问题无关的代码。
  • @Quimby 不,它只是一个变量名,将传递给 corr.sample(position_array)。你是说 position_array 应该先初始化一个数组吗?
  • 不不,这不是 Python,变量具有类型,并且这些类型在其生命周期内无法更改。所以如果first_cells 返回某个类型,position_array 必须有兼容的类型,或者使用auto。我不知道在暗示什么,因为我不知道你想要什么样的数组。基于first_cells 我猜std::vector&lt;float&gt; position_array 但我不知道cor::sample 接受什么类型。
  • 如果您只想将其传递给其他地方,那么auto position_array = first_cells(); 会为您推断出正确的类型。
  • cor::sample 接受双精度类型。我通过传递双随机数进行了检查。

标签: c++ boost


【解决方案1】:
double position_array = first_cells();

这会尝试将first_cells(一个向量)作为可调用对象调用(但它没有实现operator():https://en.cppreference.com/w/cpp/container/vector)。

假设循环变量是有原因的,为什么不使用它:

double position_array = first_cells[i];

请注意

  • 这会将float 转换为double 可能不需要

  • 如果你想要边界检查,使用

      double position_array = first_cells.at(i);
    

position_array 这个名字确实暗示了一些混乱。您真的想要三值元组单元格而不是第一个值吗?


获取第一行而不是第一个单元格:

auto       ds         = open_dataset(argv[1]);
array_2d_t first_rows = row_from_all_frames(ds, 0);

你可以使用的:

size_t     nsamples   = first_rows.size();

std::cout << "no. of samples: " << nsamples << "\n";

// main loop
for (size_t i = 1; i < nsamples; ++i) {
    auto position_array = first_rows[i];
    // double position_array = static_cast<double>(std::rand()) / RAND_MAX;
    std::cout << "position arrays: "       //
              << position_array[0] << ", " //
              << position_array[1] << ", " //
              << position_array[2] << std::endl;
    // append data to the correlator, which possibly computes some time
    // correlations
    //corr.sample(position_array);
}

注意position_arraymulti_array的子数组视图,所以接口与multi_array类型一致。

在我的系统上打印:

no. of samples: 75
position arrays: 80.03, 35.42, 4.35
position arrays: 80.19, 35.62, 4.27
position arrays: 79.78, 35.68, 4.13
position arrays: 79.51, 35.93, 4.1
position arrays: 79.44, 35.46, 4.27
position arrays: 79.5, 35.43, 4.38
position arrays: 79.03, 35.72, 4.54
position arrays: 79.12, 35.89, 4.28
position arrays: 79.04, 36.35, 3.99
position arrays: 79.06, 36.16, 4.52
position arrays: 79.22, 35.96, 4.39
position arrays: 79.07, 35.84, 4.28
position arrays: 79.43, 35.09, 4.4
position arrays: 79.38, 35.13, 3.81
position arrays: 78.87, 35.73, 4.54
position arrays: 79.3, 35.82, 4.33
position arrays: 79.38, 35.45, 3.98
position arrays: 79.5, 35.48, 3.88
position arrays: 79.16, 34.93, 4.35
position arrays: 78.86, 35.2, 4.44
position arrays: 79.15, 35.53, 4.08
position arrays: 79.41, 35.67, 3.87
position arrays: 79.83, 35.61, 4.19
position arrays: 79.63, 35.26, 3.86
position arrays: 79.94, 35.42, 4.11
position arrays: 80.32, 35.06, 4.01
position arrays: 79.99, 35.44, 3.97
position arrays: 79.82, 35.31, 4.07
position arrays: 80, 34.97, 4.07
position arrays: 80.22, 35.07, 3.91
position arrays: 80.38, 35.56, 3.92
position arrays: 80.6, 35.14, 4.11
position arrays: 80.57, 34.93, 4.15
position arrays: 80.05, 35.33, 4.46
position arrays: 80.12, 35.21, 4.2
position arrays: 80.12, 35.39, 3.97
position arrays: 80.19, 35.69, 4.18
position arrays: 80.4, 35, 3.96
position arrays: 80.55, 35.39, 4.26
position arrays: 80.52, 34.85, 4.07
position arrays: 80.57, 34.66, 4.04
position arrays: 80.69, 34.64, 4.05
position arrays: 80.94, 34.53, 3.88
position arrays: 81.12, 33.99, 4.22
position arrays: 81.25, 34.02, 4.08
position arrays: 81.68, 33.82, 4.17
position arrays: 81.75, 33.89, 4.35
position arrays: 82.2, 34.24, 4.28
position arrays: 81.83, 34.51, 4.17
position arrays: 82.17, 34.09, 4.35
position arrays: 82.33, 34.32, 4.3
position arrays: 82.65, 34.35, 4.09
position arrays: 82.44, 34.6, 4
position arrays: 82.51, 34.04, 4.41
position arrays: 82.4, 34.45, 4.34
position arrays: 81.89, 34.48, 4.18
position arrays: 81.59, 34.62, 4.14
position arrays: 81.82, 34.22, 4.34
position arrays: 81.43, 33.95, 4.05
position arrays: 81.35, 33.88, 3.9
position arrays: 81.44, 33.85, 4.24
position arrays: 81.48, 33.39, 4.25
position arrays: 81.51, 33.69, 4.16
position arrays: 81.66, 33.49, 4.34
position arrays: 82.1, 33.45, 4.17
position arrays: 82.61, 33.8, 4.07
position arrays: 82.51, 33.96, 4.5
position arrays: 82.36, 34.13, 4.46
position arrays: 82.46, 34.28, 4.19
position arrays: 82.59, 34, 4.17
position arrays: 82.26, 33.92, 4.44
position arrays: 82.2, 34.06, 4.18
position arrays: 82.24, 34.12, 4.29
position arrays: 82.16, 33.39, 3.94

【讨论】:

  • 感谢您的回答!最后我需要三值元组,但如果你认为你现在可以建议如何实现,那就完美了。
  • 添加了row_from_all_frames(ds, 0)的另一个草图
  • 非常感谢!!这样可以节省很多时间!
  • 我不太确定。我仍然有强烈的感觉,我正在帮助你比你自己更快地跌倒在错误的方向上。除非您更多地关注“什么”而不是“如何”,否则我无法解决这个问题。并开始清楚地描述需求。
  • 这些天我有一些时间限制来真正深入了解这门语言!但是已经学到了很多东西,例如,如何使用 API、一些 boost 库和 STL。我希望我未来的问题将更多地集中在“什么”而不是“如何”上! :)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2014-04-26
  • 2020-12-27
  • 1970-01-01
  • 2011-06-21
  • 1970-01-01
  • 1970-01-01
  • 2019-01-31
相关资源
最近更新 更多