简体   繁体   English

在for循环中限制vector.size()

[英]limit vector.size() in a for loop

I'm trying to do this in a vector composed of Vec3f: 我试图在由Vec3f组成的向量中执行此操作:

for (size_t i = 0; i < (mPoints.size() - 10) ; i++) {
    glVertex3f(mPoints[i]);
}

But I always get a run time error EXC_BAD_ACCESS ... if instead of size() I use an int I get the same error. 但我总是得到运行时错误EXC_BAD_ACCESS ...如果不是size()我使用int我得到相同的错误。 If instead I simply use size() without the substraction, it works fine... 如果相反我只使用size()而没有减法,它工作得很好......

What's going on? 这是怎么回事?

It sounds like you are possibly running into a problem where the vector has less than 10 elements in it. 听起来你可能遇到的问题是向量中的元素少于10个。 So when you subtract 10 from an unsigned value it's possible for the implementation to wrap around and give you a very large number (basically max - 10). 因此,当你从无符号值中减去10时,实现可能会回滚并给你一个非常大的数字(基本上是max - 10)。 Hence you try and iterate past the bounds of the vector. 因此,您尝试迭代超过向量的边界。

Try the following instead 请尝试以下方法

for (size_t i = 0; i + 10 < vector.size(); i++) {
  glVertex3f(mPoints[i]);
} 

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

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