简体   繁体   English

查找 glm::vec3 数组的大小

[英]Find size of glm::vec3 array

I have this glm::vec3 array and I would like to know the size of the array:我有这个glm::vec3数组,我想知道数组的大小:

glm::vec3 cubePositions[] = {
        glm::vec3(0.0f,  0.0f,  0.0f),
        glm::vec3(2.0f,  5.0f, -15.0f),
        glm::vec3(-1.5f, -2.2f, -2.5f),
        glm::vec3(-3.8f, -2.0f, -12.3f),
        glm::vec3(2.4f, -0.4f, -3.5f),
        glm::vec3(-1.7f,  3.0f, -7.5f),
        glm::vec3(1.3f, -2.0f, -2.5f),
        glm::vec3(1.5f,  2.0f, -2.5f),
        glm::vec3(1.5f,  0.2f, -1.5f),
        glm::vec3(-1.3f,  1.0f, -1.5f)
    };  

So far the size is 10 but is there a way to find that in code?到目前为止,大小为 10,但有没有办法在代码中找到它?

I have tried cubePositions->length() but it only gives me the value 3.我试过cubePositions->length()但它只给了我 3 的值。

I have tried glm::length(cubePositions) but it doesn't seem like thats how you use the glm::length function.我已经尝试过glm::length(cubePositions)但它似乎不是你使用glm::length函数的方式。 This way gives an error.这种方式会出错。

Thank you for any help!感谢您的任何帮助!

cubePositions is a C style array. cubePositions是一个 C 风格的数组。 It decays to a pointer to glm::vec3 .它衰减为指向glm::vec3的指针。 See here about array to pointer decay: What is array to pointer decay?请参阅此处有关数组到指针衰减的信息: 什么是数组到指针衰减? . .

Therefore when you use cubePositions->length() it's equivalent to using cubePositions[0].length() , which returns the glm length of the first element (ie 3 because it's a vec3 ).因此,当您使用cubePositions->length()时,它等效于使用cubePositions[0].length() ,它返回第一个元素的 glm 长度(即 3 因为它是vec3 )。

To get the number of elements in the array in this case you can use:在这种情况下,要获取数组中的元素数,您可以使用:

auto num_elements = sizeof(cubePositions) / sizeof(cubePositions[0]);

Note: this will work only in the scope where cubePositions is defined because only there the real size of the array is known.注意:这仅在定义cubePositions的范围内有效,因为只有那里知道数组的实际大小。 It will no longer work eg if you pass cubePositions to another function, because then the array will decay to a pointer and will "loose" it's sizeof.例如,如果您将cubePositions传递给另一个函数,它将不再起作用,因为这样数组将衰减为指针并“松散”它的 sizeof。

However - this is not the recomended way to do it in C++.但是- 这不是在 C++ 中推荐的方法。
It is better to use std::vector for a dynamic size array.最好将std::vector用于动态大小的数组。 It has a method: size() to retrieve the number of elements:它有一个方法: size()来检索元素的数量:

#include <vector>

std::vector<glm::vec3> cubePositions = {
    glm::vec3(0.0f,  0.0f,  0.0f),
    glm::vec3(2.0f,  5.0f, -15.0f),
    glm::vec3(-1.5f, -2.2f, -2.5f),
    glm::vec3(-3.8f, -2.0f, -12.3f),
    glm::vec3(2.4f, -0.4f, -3.5f),
    glm::vec3(-1.7f,  3.0f, -7.5f),
    glm::vec3(1.3f, -2.0f, -2.5f),
    glm::vec3(1.5f,  2.0f, -2.5f),
    glm::vec3(1.5f,  0.2f, -1.5f),
    glm::vec3(-1.3f,  1.0f, -1.5f)
};

auto num_elements = cubePositions.size();

Note: if your array has a fixed size (known at compile time), you can also use std::array .注意:如果您的数组具有固定大小(在编译时已知),您也可以使用std::array It also has a size() method, although in this case it is not as useful, as the size cannot change dynamically and is actually one of the template parameters.它也有一个size()方法,尽管在这种情况下它没有那么有用,因为大小不能动态改变并且实际上是模板参数之一。


Update: :更新::
As @HolyBlackCat commented below , it is better to use std::size to get the number of elements in an old C array:正如@HolyBlackCat 在下面评论的那样,最好使用std::size来获取旧 C 数组中的元素数:

auto num_elements = std::size(cubePositions);

It will not compile if you pass a decayed pointer rather than a real array.如果你传递一个衰减的指针而不是一个真正的数组,它将不会编译。 Just keep in mind that as I wrote above you'd better avoid old C arrays if you can.请记住,正如我在上面写的那样,如果可以的话,最好避免使用旧的 C 数组。

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

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