iterator default value [modified]
-
How can I check whether an iterator was assigned a value or not? In other words, if I create an iterator with default constructor what value will it present which I could check against? It is not the trivial case such this:
list::iterator it; //what does it contains?
but this:
map::iterator> aci;
AClass *ptr=someinitializedaclassptr;
if(aci[ptr]==UNINITIALIZED_ITERATOR)
{
dosomething();
}In above example issuing
aci[ptr]
creates already an entry in the map with default constructed valuelist::iterator
if there isn't a key of valueptr
.modified on Friday, August 5, 2011 7:48 AM
-
How can I check whether an iterator was assigned a value or not? In other words, if I create an iterator with default constructor what value will it present which I could check against? It is not the trivial case such this:
list::iterator it; //what does it contains?
but this:
map::iterator> aci;
AClass *ptr=someinitializedaclassptr;
if(aci[ptr]==UNINITIALIZED_ITERATOR)
{
dosomething();
}In above example issuing
aci[ptr]
creates already an entry in the map with default constructed valuelist::iterator
if there isn't a key of valueptr
.modified on Friday, August 5, 2011 7:48 AM
Read this thread on (ahem) stackoverflow: http://stackoverflow.com/questions/3395180/what-is-an-iterators-default-value[^]
Steve
-
Read this thread on (ahem) stackoverflow: http://stackoverflow.com/questions/3395180/what-is-an-iterators-default-value[^]
Steve
Well, it is obvious it has no special value as
int val;
. The problem is how to check this. I'll try the below code:#include "list"
#include "map"
#include "iostream"using namespace std;
int main()
{
list::iterator it,itnull;
map::iterator> imap;it = imap\[1\]; //map creates entry with key '1' and default constructed iterator if(it==itnull) //checking with other default constructed iterator cout<<"it unitialized"<