【发布时间】:2020-02-19 09:02:30
【问题描述】:
这是我的代码。
std::vector<int> x;
std::vector<int> y;
void vector_pile()
{
if (x.size() > 0 && y.size() > 0)
{
// clear the x & y movement vectors so we can update it to the current movement vector
x.clear();
y.clear();
}
// set vector size
x.resize(WeaponBulletCount());
y.resize(WeaponBulletCount());
// updates pattern to move vector to reduce strain on program calculations
if (WeaponBulletCount() > 0) {
for (int i = 0; i <= WeaponBulletCount(); i++)
{
x.push_back(i);
y.push_back(i);
std::cout << "x: " << x[i] << std::endl;
std::cout << "y: " << y[i] << std::endl;
}
}
Sleep(1000);
}
函数WeaponBulletCount();返回值30。这个函数的主要原因是有一个向量可以在变量发生变化时改变,从而减少重复计算的次数。
【问题讨论】:
-
您将
reserve与resize混淆了。 -
您是否尝试预先分配具有大小的向量,然后将值直接分配到现有索引中?我不明白你问了什么..
-
旁白:您可以将 x 简化为
x.resize(WeaponBulletCount()); std::iota(x.begin(), x.end(), 0); -
for 循环中的条件语句看起来像一个非一错误。应该是
i < WeaponBulletCount()。