PDA

View Full Version : ...deceptively simple...



Sebastiani
07-29-2002, 12:51 PM
Ponder this simple yet useful class...







class Iter {

double current, count;

public:

Iter(int _count = 1):count(_count)
{
current = 0;
}

bool True()
{
if( current == count ) return false;

current++;

return true;
}

void False()
{
current = count;
}

bool IsTrue(){ return current != count; }

bool IsFalse(){ return current == count; }

void Reset(int resetTo = 0) { current = resetTo; }
};







...assume we have problems with some values in an array, and need to debug...









int main()
{

int array[100000];

//...process array...

int x;
Iter i;


for(x = 0; x < 100000; x++)
{
if(array[x] != expected_value)
if(i.True())
{
printf("Look-> %i", array[x]); //...prints 1 time max...
}


i.Reset(10);

for(x = 0; x < 100000; x++)
{
if(array[x] != another_expected_value)
if(i.True())
{
printf("Look-> %i", array[x]); //...prints 10 times max...
}

return 0;
}






I have been using this simple class for smart booleans, quick iterators, and initializers.
Though simple, it is a nice addition to any toolbox.
;)