Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using an STL Iterator without initialising it

I would like to do something like this:

container::iterator it = NULL;

switch ( eSomeEnum )
{
case Container1:
it = vecContainer1.begin();
break;

case Container2:
it = vecContainer2.begin();
break;
...


}

for( ; it != itEnd ; ++it )
{ 
..
}

But I can't create and initialise an iterator to NULL. Is there some way I can do this? Ideally I would just create and assign the iterator in the switch, but then it would go out of scope immediately.

like image 538
Konrad Avatar asked Jul 21 '26 15:07

Konrad


2 Answers

You just needn't initialize it at all, because iterators are DefaultConstructible.

like image 124
jpalecek Avatar answered Jul 24 '26 05:07

jpalecek


All you should need to do is change

container::iterator it = NULL;

to

container::iterator it;

and I think your code will work as intended.

like image 24
Evan Shaw Avatar answered Jul 24 '26 05:07

Evan Shaw