C++ C++;。更新和复制每个帧的一系列值。我应该使用哪种类类型?
我在每一帧生成一个值集合,我想将这些值添加到一个特定索引的更大的值集合中 这是我正在生成的集合的典型示例C++ C++;。更新和复制每个帧的一系列值。我应该使用哪种类类型?,c++,arrays,C++,Arrays,我在每一帧生成一个值集合,我想将这些值添加到一个特定索引的更大的值集合中 这是我正在生成的集合的典型示例 std::vector<glm::vec3> corners; corners.reserve(8); //glm::vec3 corners[8]; //std::list<glm::vec3> corners; corners[i++] = position - hX + hY + hZ; corners[i++] = position + hX + hY +
std::vector<glm::vec3> corners;
corners.reserve(8);
//glm::vec3 corners[8];
//std::list<glm::vec3> corners;
corners[i++] = position - hX + hY + hZ;
corners[i++] = position + hX + hY + hZ;
corners[i++] = position + hX - hY + hZ;
corners[i++] = position - hX - hY + hZ;
corners[i++] = position - hX + hY - hZ;
corners[i++] = position + hX + hY - hZ;
corners[i++] = position + hX - hY - hZ;
corners[i++] = position - hX - hY - hZ;
我可以使用什么样的类类型来高效地生成较小的集合并将其复制到较大的集合,而不需要在每个帧生成该集合时产生太多开销
我可以将每个新生成的较小集合分配到较大集合的末尾,这样索引值就可以忽略。在代码中,您应该
角点。调整大小(8)
,而不是角点。保留(8)
。
此外,如果我理解正确的话,你似乎总是有8个角?
然后使用数组:
typedef std::array<glm::vec3, 8> Corners;
// or in pre-c++0B code use boost::array same way
// or if neither is available then just raw glm::vec3[8] can do.
可以使用标准副本进行复制:
std::vector<glm::vec3> vertices;
// somehow it gets filled
// somewhere comes index
std::copy( corners.begin(), corners.end(), vertices.begin() + index );
// with raw glm::vec3[8] std::copy( corners, corners + 8, vertices.begin() + index );
std::向量顶点;
//不知怎的,它被填满了
//某处来了索引
复制(corners.begin()、corners.end()、顶点.begin()+索引);
//使用原始glm::vec3[8]std::copy(角点,角点+8,顶点.begin()+索引);
如果需要插入而不是复制,请使用顶点。插入()您可以使用。如果要在向量顶点中的位置索引
之前插入角点
中的值,请执行以下操作:
vertices.insert(vertices.begin() + index, corners.begin(), corners.end());
但是,该操作需要为新值留出空间。这会将所有元素从索引
移动到向量的末尾(可能还需要调整向量的大小)
为了避免这种情况,您可以使用一个数组来代替。难道没有一个std::vector.insert()可以这样做吗?为什么不使用数组呢?只需将角落
存储在某个地方,而不是每次都创建;我在std::vector.insert()方面遇到问题,因为我不希望动态调整数组的大小。使用vertices.assign(maxVertices,glm::vec3())初始化它代码>在使用insert时也会导致问题。我得到的向量下标超出范围错误。是否要在顶点中的位置索引处插入角的元素,或者要复制值(因此,覆盖顶点中的一些值)?这非常好,谢谢。我想不会有太多的过度改变每个帧的角点元素?std::vector由于其动态大小而有一些开销。std::array和boost::array的性能与原始阵列一样好。
// somehow we have position, hX, hY and hZ
Corners corners =
{
position - hX + hY + hZ,
position + hX + hY + hZ,
position + hX - hY + hZ,
position - hX - hY + hZ,
position - hX + hY - hZ,
position + hX + hY - hZ,
position + hX - hY - hZ,
position - hX - hY - hZ
};
std::vector<glm::vec3> vertices;
// somehow it gets filled
// somewhere comes index
std::copy( corners.begin(), corners.end(), vertices.begin() + index );
// with raw glm::vec3[8] std::copy( corners, corners + 8, vertices.begin() + index );
vertices.insert(vertices.begin() + index, corners.begin(), corners.end());