使用数组向量的正确方法
有人能告诉我使用数组向量的正确方法是什么吗?
我声明了一个数组向量 (vector
),但收到错误:请求从 'int' 到非标量类型 'float [4]' 的转换
当尝试调整大小
时。出了什么问题?
Could someone tell what is the correct way to work with a vector of arrays?
I declared a vector of arrays (vector<float[4]>
) but got error: conversion from 'int' to non-scalar type 'float [4]' requested
when trying to resize
it. What is going wrong?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
您不能将数组存储在
向量
或任何其他容器中。要存储在容器中的元素的类型(称为容器的值类型)必须既可复制构造又可赋值。数组都不是。但是,您可以使用
array
类模板,例如 Boost、TR1 和 C++0x 提供的模板:(您需要替换
std::array
与std::tr1::array
来使用 C++ TR1 中包含的模板,或使用boost::array
来使用 Boost 库中的模板。或者,您也可以编写自己的模板;这非常简单。)You cannot store arrays in a
vector
or any other container. The type of the elements to be stored in a container (called the container's value type) must be both copy constructible and assignable. Arrays are neither.You can, however, use an
array
class template, like the one provided by Boost, TR1, and C++0x:(You'll want to replace
std::array
withstd::tr1::array
to use the template included in C++ TR1, orboost::array
to use the template from the Boost libraries. Alternatively, you can write your own; it's quite straightforward.)使用:
Use:
以下代码段没有错误:
OUTPUT IS:
6.28
2.5
9.73
4.364
结论:
是除了
James McNellis 所建议的另一种可能性。
There is no error in the following piece of code:
OUTPUT IS:
6.28
2.5
9.73
4.364
IN CONCLUSION:
is another possibility apart from
that James McNellis suggested.
矢量的每个元素都是
float[4]
,因此当您调整大小时,每个元素都需要默认从float[4]
初始化。我认为您尝试使用像0
这样的int
值进行初始化?尝试:
Every element of your vector is a
float[4]
, so when you resize every element needs to default initialized from afloat[4]
. I take it you tried to initialize with anint
value like0
?Try:
自 C++11 起,对向量元素的唯一一般要求是它满足 使用所用分配器的可擦除要求。它基本上要求可以通过正确的反弹 std::allocator_traits::destroy 来销毁对象类型。
对于标准分配器
std::allocator
,在 C++20 之前无法满足此要求,因为std::allocator::destroy
会尝试使用简单的(伪-)析构函数调用,其格式良好仅适用于类和标量类型,不适用于数组类型。由于 C++20
std::allocator
的destroy
默认通过std::allocator_traits
使用std::destroy_at< /code> 和
std::destroy_at
通过在数组元素上递归调用自身来扩展以支持数组类型。因此,数组类型现在可以满足 Erasable 要求,并且通常不再禁止在带有默认分配器的std::vector
中使用数组类型。但是,在实际构造向量中的元素时,向量必须使用分配器的构造函数,C++20 也默认使用 std::construct_at。不幸的是的唯一方法是构造一个空实例。
std::construct_at
不适用于数组,这可能是无意的(请参阅开放的 LWG 问题第3436章因此,目前不可能使用默认分配器在此类向量中构造任何元素,并且使用 std::vector即使这个问题得到解决,这样的向量也几乎没有用处。人们可以用它做的唯一一件事就是构造一个具有给定数量的值初始化元素的实例,然后对该固定大小的向量元素进行操作。无法使用任何修饰符,因为它们需要某种形式的元素类型复制或移动,而这对于数组类型来说是不可能的。这样的向量可以移动和交换,但不能复制。
Since C++11 the only general requirement on the element of a vector is that it satisfies the Erasable requirement with the used allocator. It basically requires that the object type can be destroyed through a properly rebound
std::allocator_traits::destroy
.With the standard allocator
std::allocator
, this requirement was not satisfied before C++20, becausestd::allocator::destroy
would try to use a simple (pseudo-)destructor call which is well-formed only for class and scalar types, not for array types.Since C++20
std::allocator
'sdestroy
is defaulted throughstd::allocator_traits
to usestd::destroy_at
andstd::destroy_at
is extended to support array types by calling itself recursively on the array elements. So the Erasable requirement is now fulfilled with array types and it is not generally forbidden to use array types instd::vector
with the default allocator anymore.However, when actually constructing elements in the vector, the vector must use the allocator's
construct
, which was also defaulted with C++20 to usestd::construct_at
. Unfortunatelystd::construct_at
doesn't work with arrays, which is probably unintentional (see open LWG issue 3436). So currently it is not possible to construct any elements in such a vector with the default allocator and the only way to usestd::vector<float[4]>
is to construct an empty instance.Even when this issue is resolved, there is however almost no use for such a vector. The only thing one can do with it is constructing an instance with a given number of value-initialized elements and then operate on this fixed-size vector's elements. None of the modifiers can be used because they require some form of copying or moving of the element type, which isn't possible for array types. Such a vector can be moved and swapped, but not copied.