How to check iterator for NULL
-
vector<int> vect;
vector<int>::iterator pBegin;I want to check the iterator for NULL. In VC6 we can compare if (pBegin == NULL) and it works. But in VC10 we cannot compare iterator to NULL. How to achieve that in VC10?
You should check against
begin
orend
(usually past theend
). if you haveKASR1 wrote:
vector<int>::iterator pBegin;
Than it should be treated as an uninitialized variable, so it it give out bad results if used as-is. As far as I know, the STL implementation in VC6 is crap and not standard.
Watched code never compiles.
-
You should check against
begin
orend
(usually past theend
). if you haveKASR1 wrote:
vector<int>::iterator pBegin;
Than it should be treated as an uninitialized variable, so it it give out bad results if used as-is. As far as I know, the STL implementation in VC6 is crap and not standard.
Watched code never compiles.
-
Ok. Lets assume its not initialized. Can we consider checking if (pBegin != vect.end()) in place of if( pBegin != NULL)
You need to assign the iterator to something valid before using it.
std::vector v;
// fill the vector.std::vector::iterator pBegin = v.begin();
while ( pBegin != v.end() )
{
// do something.
++it;
}or
if ( pBegin != v.end() )
{
// ite
}Watched code never compiles.
-
You need to assign the iterator to something valid before using it.
std::vector v;
// fill the vector.std::vector::iterator pBegin = v.begin();
while ( pBegin != v.end() )
{
// do something.
++it;
}or
if ( pBegin != v.end() )
{
// ite
}Watched code never compiles.
-
You need to assign the iterator to something valid before using it.
std::vector v;
// fill the vector.std::vector::iterator pBegin = v.begin();
while ( pBegin != v.end() )
{
// do something.
++it;
}or
if ( pBegin != v.end() )
{
// ite
}Watched code never compiles.
-
vector<int> vect;
vector<int>::iterator pBegin;I want to check the iterator for NULL. In VC6 we can compare if (pBegin == NULL) and it works. But in VC10 we cannot compare iterator to NULL. How to achieve that in VC10?
Checking it against NULL is wrong. It's an implementation detail that it works in vc6 (because raw pointers are used, amongst other implementation specific details). Iterators are not required be pointers, in fact vectors are one of the few containers where they can do the job and as you've discovered even then they are not the only option.
Steve