find() 和 distance() 的准确度如何
how accurate is find() and distance()
我想我在某处读到 distance()
当 return 迭代器位置可能很挑剔。有时它 return 的位置不正确。我想知道这是不是真的,或者我是否没有正确使用它。
我正在尝试查找 21 向量中的粒子何时悬停。这个想法是一旦有人悬停就切换其他人的状态。
我正在使用 find()
来了解粒子何时悬停,因此是正确的。
vector<bool>::iterator it;
it = find(_tmp->isParticleHovered.begin(), _tmp->isParticleHovered.end(), true);
if (it != _tmp->isParticleHovered.end()){// we look if a particle is being hovered.
isHovered = true;// we use this to check internally the first boid
}else{
isHovered = false;
}
现在我不仅想知道悬停的时间,还想知道悬停的是哪一个,所以我添加了这个:
vector<bool>::iterator it;
it = find(_tmp->isParticleHovered.begin(), _tmp->isParticleHovered.end(), true);
if (it != _tmp->isParticleHovered.end()){// we look if a particle is being hovered.
l = distance(_tmp->isParticleHovered.begin(), it);
isHovered = true;// we use this to check internally the first boid
}else{
isHovered = false;
l = -1;
}
所以知道了索引,我想切换其他人的状态,所以我想到了以下内容:
if ( l == -1){
if ( boidState5){
resetFamilyBoidState(_tmp);// makes all the particles go back to the same state
boidState2 = true;
boidState5 = false;
}
}else if ( l != -1){
if ( boidState2 ){
makeBoidStateless(_tmp, l);// I pass L, to this function, tell the function to switch all the particles to a different state except the one that is being hovered.
boidState5 = true;
boidState2 = false;
}
}
它会工作几次,但是当我快速地从一个粒子悬停到另一个粒子时,它会变得混乱,有时 l
会 return 21 这会使它崩溃,因为粒子矢量大小为 21 - 最新容器为 20。
我想出了一个既不使用 find()
也不使用 distance()
的解决方案:
int FamiliesController::returnInfoBoxState(){
for ( int i = 0; i < boidList.size(); i++){
if ( boidList[i]->boidState == 2){
return i;
}
}
return -1;
}
在控制器 class 中,我创建了一个函数,该函数会 return 在调用特定状态时向我提供索引号,否则它会 return -1。使用相同的 if 语句它工作正常。
我很想知道 find()
和 distance()
。非常感谢任何澄清。
std::distance
是准确的。毫无疑问。
不过,您很可能误解了它的功能。你说的可能return "wrong position"。无论如何,它从来没有 returns 位置。迭代器是一个位置。
此外,您可能需要查看 std::bitset<21>
。当位数固定的时候比较合适,还有额外的辅助函数比如.reset()
我想我在某处读到 distance()
当 return 迭代器位置可能很挑剔。有时它 return 的位置不正确。我想知道这是不是真的,或者我是否没有正确使用它。
我正在尝试查找 21 向量中的粒子何时悬停。这个想法是一旦有人悬停就切换其他人的状态。
我正在使用 find()
来了解粒子何时悬停,因此是正确的。
vector<bool>::iterator it;
it = find(_tmp->isParticleHovered.begin(), _tmp->isParticleHovered.end(), true);
if (it != _tmp->isParticleHovered.end()){// we look if a particle is being hovered.
isHovered = true;// we use this to check internally the first boid
}else{
isHovered = false;
}
现在我不仅想知道悬停的时间,还想知道悬停的是哪一个,所以我添加了这个:
vector<bool>::iterator it;
it = find(_tmp->isParticleHovered.begin(), _tmp->isParticleHovered.end(), true);
if (it != _tmp->isParticleHovered.end()){// we look if a particle is being hovered.
l = distance(_tmp->isParticleHovered.begin(), it);
isHovered = true;// we use this to check internally the first boid
}else{
isHovered = false;
l = -1;
}
所以知道了索引,我想切换其他人的状态,所以我想到了以下内容:
if ( l == -1){
if ( boidState5){
resetFamilyBoidState(_tmp);// makes all the particles go back to the same state
boidState2 = true;
boidState5 = false;
}
}else if ( l != -1){
if ( boidState2 ){
makeBoidStateless(_tmp, l);// I pass L, to this function, tell the function to switch all the particles to a different state except the one that is being hovered.
boidState5 = true;
boidState2 = false;
}
}
它会工作几次,但是当我快速地从一个粒子悬停到另一个粒子时,它会变得混乱,有时 l
会 return 21 这会使它崩溃,因为粒子矢量大小为 21 - 最新容器为 20。
我想出了一个既不使用 find()
也不使用 distance()
的解决方案:
int FamiliesController::returnInfoBoxState(){
for ( int i = 0; i < boidList.size(); i++){
if ( boidList[i]->boidState == 2){
return i;
}
}
return -1;
}
在控制器 class 中,我创建了一个函数,该函数会 return 在调用特定状态时向我提供索引号,否则它会 return -1。使用相同的 if 语句它工作正常。
我很想知道 find()
和 distance()
。非常感谢任何澄清。
std::distance
是准确的。毫无疑问。
不过,您很可能误解了它的功能。你说的可能return "wrong position"。无论如何,它从来没有 returns 位置。迭代器是一个位置。
此外,您可能需要查看 std::bitset<21>
。当位数固定的时候比较合适,还有额外的辅助函数比如.reset()