Hey guys, I got this weired problem with a vector<bool>::iterator,
probelm occurs when executing *it=!*it (I'm reversing the bool value, true to false, and false to true).
Here is the code:
1 2 3 4 5 6 7
void turnall(LAMP *lp)
{
for (LAMP::iterator it=lp->begin();
it!=lp->end();
it++)
*it=!*it;// program stops at this line
}
BTW could anyone tell me how to manage iterator increment that bigger than 1 step, for example in the following function, I just want to turn lamps that have an odd number, but if I do it+=2 the iterator won't meet lp->end() exactly, so it can't end properly, how could I do this?
1 2 3 4 5 6 7 8
void turnodd(LAMP *lp)
{
for (LAMP::iterator it=lp->begin();
it!=lp->end(); // would this situation be finally met?
it+=2)// 2 steps a time
*it=!*it;
}
std::vector<bool> need not meet all the requirements for a standard library sequence container; for instance, std::vector<bool>::reference is not a true lvalue reference to bool. vector<bool>::iterator need not yield a bool& when dereferenced.
1 2 3 4 5 6 7
#include <vector>
void turn_all( std::vector<bool>& vecb )
{ for( std::size_t i = 0 ; i < vecb.size() ; ++i ) vecb[i].flip() ; }
void turn_odd( std::vector<bool>& vecb )
{ for( std::size_t i = 0 ; i < vecb.size() ; i += 2 ) vecb[i].flip() ; }
> how to manage iterator increment that bigger than 1 step
If using random iterators, you may change the condition to it < lp->end()
else you could use a function