简体   繁体   English

将std :: vector分配给std :: valarray

[英]Assign a std::vector to a std::valarray

I have a vector<vector<double> , so a table (matrix) of values. 我有一个vector<vector<double> ,所以有一个值的表(矩阵)。 Columns contains position and velocity of a planet, so rows stores data of the same planet. 列包含行星的位置和速度,因此行存储同一行星的数据。 I want to transform a row in a valarray because I need math operations. 我想在valarray变换一行,因为我需要数学运算。 Then I want to store the valarrays (the planets) in a vector. 然后我想将valarrays(行星)存储在矢量中。 I tried like this: 我试过这样的:

vector<vector<double>> corps_tmp=configFile.get_corps(); // The function returns a vector<vector<double>>

    valarray<double> corpX;
    for (int i(0); i < corps_tmp.size(); i++) {
        corpX = corps_tmp[i]; // I want to save the first row of the vector<vector<double>> on the valarray
        corps.push_back(corpX); // I want to ''add'' the valarray to a vector.
        corpX = 0;
    }

This code doesn't works and I obtain an error around the assignment of a vector to a valarray (apparently not permitted). 这段代码不起作用,我在向valarray指定向量时显然是错误的(显然是不允许的)。

Is there any way to achieve in a simple way what I tried to do? 有没有办法以一种简单的方式实现我的目标?

To create a valarray from a vector: 要从矢量创建一个valarray:

std:valarray<double> corpX(corps_tmp[i].data(), corps_tmp[i].size());

To write the data back into a vector: 要将数据写回向量:

corps_tmp[i].assign(std::begin(corpX), std::end(corpX));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM