简体   繁体   中英

how accurate is find() and distance()

I think I read somewhere that distance() when returning the iterator position can be finicky. And sometimes it doesn't return the right position. I want to know if this is true or if I'm not using it right.

I'm trying to find when a particle in a vector of 21 is hovered. The idea is to switch the state of the others once one gets hovered.

I'm using find() to know when the particle is hovered, hence true.

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;

    }

Now I also wanted to know not only when it was hovered but which one was hovered so I added this:

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;
    }

So knowing the index, I wanted to switch the states of the others so I came up with the following:

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;
}

}

It will work for a couple of times but when I will hover from particle to particle rapidly it will get confused, and sometimes l will return 21 which will make it crash since the particle vector size is 21 - being 20 the latest container.

I came up with a solution without using neither find() nor distance() :

int FamiliesController::returnInfoBoxState(){

 for ( int i = 0; i < boidList.size(); i++){

     if ( boidList[i]->boidState == 2){

         return i;
     }
 }

 return -1;

}

In the controller class I created a function that would return me the index number when that specific state was called, otherwise it would return -1. Using the same if statement it worked fine.

I'm curious to find about find() and distance() . Any clarification is much appreciated.

std::distance is exact. There is no room for doubt.

You are most likely misunderstanding its function, though. You said that it may return the "wrong position". It never returns positions anyway. An iterator is a position.

Also, you may want to check std::bitset<21> . It's more appropriate when the number of bits is fixed, and it has extra helper functions such as .reset()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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