简体   繁体   English

如何将 std::vector 移动到 C++ 中的原始数组中

[英]How to move a std::vector into a raw array in C++

How can I move the contents of std::vector into an array safely without copying or iterating over all elements?如何在不复制或迭代所有元素的情况下安全地将std::vector的内容移动到数组中?

void someFunc(float* arr, std::size_t& size)
{
   std::vector<float> vec(5, 1.5f);
   // do something with the data

   size = vec.size();
   arr = vec.data(); // this doesn't work, as at the end of the function the data in std::vector will be deallocated

}

main()
{
   float* arr;
   std::size_t size{0};

   someFunc(arr, size);

   // do something with the data
   delete [] arr;
}

How can I assign my array with the data in std::vector and making sure that deallocation will not be called on std::vector ?如何为我的数组分配std::vector中的数据并确保不会在std::vector上调用解除分配? Or maybe any other ideas to go around this?或者可能对 go 有任何其他想法?

You can't.你不能。

A vector owns its buffer.一个向量拥有它的缓冲区。 You cannot steal it.你不能偷它。

You will have to copy/move the elements individually, optionally using a helper algorithm that does the iteration for you ( std::copy / std::move ).您必须单独复制/移动元素,可选择使用为您执行迭代的辅助算法( std::copy / std::move )。

(Also note that, since your element type is just float , a move here is a copy.) (另请注意,由于您的元素类型只是float ,因此此处的移动副本。)

(Also note that this std::move , the algorithm, is not the same as std::move , the rvalue cast.) (另请注意,此算法std::move std::move与右值转换 std::move 不同。)


Consider whether you really need to do this.考虑你是否真的需要这样做。 You can treat the vector's data as an array using vec.data() whenever you need to, as long as you keep the vector alive.只要您保持向量处于活动状态,您就可以在需要时使用vec.data()将向量的数据视为一个数组。 Surely that's better than sacrificing RAII?这肯定比牺牲 RAII 更好?

This would have been handy for me for stealing a "typed" vector contents into an "untyped" vector.这对我来说很方便将“类型化”向量内容窃取到“非类型化”向量中。

This is what I want to do:这就是我想要做的:

std::vector<uint8_t>­one;
std::vector<V> other = std::move(one);

When I know that the contents are exactly what I expect (there is a runtime type info field)当我知道内容正是我所期望的(有一个运行时类型信息字段)

I would use this to do 'move semantics' between an untyped and a typed class where part of the underlying representation is in a vector - just in the typed case it is vector of Vs.我将使用它在无类型和有类型 class 之间进行“移动语义”,其中基础表示的一部分位于向量中 - 仅在类型化的情况下,它是 Vs 的向量。

Think of it as a different "view" of the data.将其视为数据的不同“视图”。 In general cases the data is untyped as there are special edge-cases, but after handling them via branching it would be handy to handle the "normal" cases type safe ways..在一般情况下,数据是无类型的,因为存在特殊的边缘情况,但是在通过分支处理它们之后,处理“正常”情况类型安全的方式会很方便..

I see I can implement this behaviour if I throw out std::vector and do my implementation using direct pointer arithmetic as then I can access that in move construction, but I would love to not give up on vectors...我看到如果我抛弃 std::vector 并使用直接指针算术执行我的实现,我可以实现这种行为,因为我可以在移动构造中访问它,但我不想放弃向量......

Just wanted to add this to show what kind of rationale might be behind these kind of questions and I think sometimes it is valid只是想添加这个以显示这些问题背后可能存在什么样的理由,我认为有时它是有效的

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

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