简体   繁体   English

std 将向量 float[3] 移动到向量 class x,y,z

[英]std move vectors float[3] into vector class x,y,z

I have a vector of 3 values in two different forms, and I would like to pass from one to the other:我在两个不同的 forms 中有一个包含 3 个值的向量,我想从一个传递到另一个:

  • a std::vector< float[3] > , std::vector< std::vector<float> > or even float[][3] if needed (that's the part I can change)一个std::vector< float[3] >std::vector< std::vector<float> >甚至float[][3]如果需要(这是我可以改变的部分)
  • a std::vector of the color class color class 的std::vector

Let's work with these classes:让我们使用这些类:

class color{
    float x,y,z;
    //some methods but no other member
}

What I would like to do:我想做的事:

std::vector<float[3]> myVector; // already allocated and filled
std::vector<color> cv = std::move( myVector ); // of course does not compile

My issue is that I didn't figure out how to convert one to the other without having to reallocate memory.我的问题是我不知道如何将一个转换为另一个,而不必重新分配 memory。 I can convert a float[3] into color using static_cast but it seems it does not work for a vector.我可以使用static_castfloat[3]转换为color ,但它似乎不适用于矢量。

I would like not to iterate over the whole array and simply move the data from myVector inside the cv vector.我不想遍历整个数组,而只是将myVector中的数据移动到cv向量中。 I thought converting the pointer of myVector to void* and then to color3* could work, but I didn't succeed.我认为将myVector的指针转换为void*然后转换为color3*可以工作,但我没有成功。

Ideas?想法?

You cannot have a std::vector<float[3]> at all prior to C++20 because float[3] didn't meet the requirements of the value type that std::vector had.在 C++20 之前,您根本不能拥有std::vector<float[3]> ,因为float[3]不符合std::vector所具有的值类型的要求。 Since C++20 it technically is allowed, but is still not useful in practice since any member function that adds elements does still have requirements not satisfied by arrays.由于 C++20 在技术上是允许的,但在实践中仍然没有用,因为任何添加元素的成员 function 仍然具有 arrays 不满足的要求。

Let's assume that you were asking a similar question where you have two different classes, where one is color and the other contains an array as a member.假设您在问一个类似的问题,其中有两个不同的类,一个是color ,另一个包含一个数组作为成员。 Such as std::array<float, 3> .比如std::array<float, 3>

I would like not to iterate over the whole array and simply move the data from myVector inside the cv vector.我不想遍历整个数组,而只是将 myVector 中的数据移动到 cv 向量中。

There is simply no way to avoid that.根本没有办法避免这种情况。 The only way to "move" data into a vector without iteration is to move from another vector of the same type.无需迭代即可将数据“移动”到向量中的唯一方法是从另一个相同类型的向量中移动。 That singular case doesn't apply to your case of different types, so you must have iteration.该单一案例不适用于您的不同类型案例,因此您必须进行迭代。 You don't necessarily need to write the loop yourself, as you may use a standard algorithm instead.您不一定需要自己编写循环,因为您可以使用标准算法。 Here is an example using standard ranges:以下是使用标准范围的示例:

std::vector<std::array<float,3>> myVector; // already allocated and filled
std::vector<color> cv;
auto arr_to_color = [](const std::array<float,3>& arr){
    // assuming such constructor exists
    return color(
        arr[0],
        arr[1],
        arr[2]);
};
std::ranges::transform(
    myVector,
    std::back_inserter(cv),
    arr_to_color);

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

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