使用数组向量的正确方法

发布于 2024-10-10 18:09:29 字数 173 浏览 3 评论 0原文

有人能告诉我使用数组向量的正确方法是什么吗?

我声明了一个数组向量 (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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

御弟哥哥 2024-10-17 18:09:29

您不能将数组存储在向量或任何其他容器中。要存储在容器中的元素的类型(称为容器的值类型)必须既可复制构造又可赋值。数组都不是。

但是,您可以使用 array 类模板,例如 Boost、TR1 和 C++0x 提供的模板:(

std::vector<std::array<double, 4> >

您需要替换 std::arraystd::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:

std::vector<std::array<double, 4> >

(You'll want to replace std::array with std::tr1::array to use the template included in C++ TR1, or boost::array to use the template from the Boost libraries. Alternatively, you can write your own; it's quite straightforward.)

ぃ弥猫深巷。 2024-10-17 18:09:29

使用:

vector<vector<float>> vecArray; //both dimensions are open!

Use:

vector<vector<float>> vecArray; //both dimensions are open!
蓝咒 2024-10-17 18:09:29

以下代码段没有错误:

float arr[4];
arr[0] = 6.28;
arr[1] = 2.50;
arr[2] = 9.73;
arr[3] = 4.364;
std::vector<float*> vec = std::vector<float*>();
vec.push_back(arr);
float* ptr = vec.front();
for (int i = 0; i < 3; i++)
    printf("%g\n", ptr[i]);

OUTPUT IS:

6.28

2.5

9.73

4.364

结论:

std::vector<double*>

是除了

std::vector<std::array<double, 4>>

James McNellis 所建议的另一种可能性。

There is no error in the following piece of code:

float arr[4];
arr[0] = 6.28;
arr[1] = 2.50;
arr[2] = 9.73;
arr[3] = 4.364;
std::vector<float*> vec = std::vector<float*>();
vec.push_back(arr);
float* ptr = vec.front();
for (int i = 0; i < 3; i++)
    printf("%g\n", ptr[i]);

OUTPUT IS:

6.28

2.5

9.73

4.364

IN CONCLUSION:

std::vector<double*>

is another possibility apart from

std::vector<std::array<double, 4>>

that James McNellis suggested.

ヅ她的身影、若隐若现 2024-10-17 18:09:29

矢量的每个元素都是 float[4],因此当您调整大小时,每个元素都需要默认从 float[4] 初始化。我认为您尝试使用像 0 这样的 int 值进行初始化?

尝试:

static float zeros[4] = {0.0, 0.0, 0.0, 0.0};
myvector.resize(newsize, zeros);

Every element of your vector is a float[4], so when you resize every element needs to default initialized from a float[4]. I take it you tried to initialize with an int value like 0?

Try:

static float zeros[4] = {0.0, 0.0, 0.0, 0.0};
myvector.resize(newsize, zeros);
对岸观火 2024-10-17 18:09:29

自 C++11 起,对向量元素的唯一一般要求是它满足 使用所用分配器的可擦除要求。它基本上要求可以通过正确的反弹 std::allocator_traits::destroy 来销毁对象类型。

对于标准分配器 std::allocator,在 C++20 之前无法满足此要求,因为 std::allocator::destroy 会尝试使用简单的(伪-)析构函数调用,其格式良好仅适用于类和标量类型,不适用于数组类型。

由于 C++20 std::allocatordestroy 默认通过 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, because std::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's destroy is defaulted through std::allocator_traits to use std::destroy_at and std::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 in std::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 use std::construct_at. Unfortunately std::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 use std::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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文