Problem with bool iterator

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
}


and the callstack status if needed:
#0 004261D6	std::_Bit_reference::operator bool(this=0x22fc10) (c:/program files/codeblocks/mingw/bin/../lib/gcc/mingw32/4.7.1/include/c++/bits/stl_bvector.h:82)
#1 00401494	turnall(lp=0x22fec4) (C:\Users\Jo\Documents\USACO\lamps\main.cpp:40)
#2 00402773	main() (C:\Users\Jo\Documents\USACO\lamps\main.cpp:240)


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() ; }
http://www.cplusplus.com/forum/general/112111/
¿what is LAMP?

> 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
1
2
3
4
5
6
7
template <class InputIterator, class Distance>
void advance (InputIterator& it, Distance n, InputIterator end){
   for(int K=0; K<n; ++K){
      if(it==end) return;
      ++it;
   }
}
Topic archived. No new replies allowed.