STL iterators

Hi guys,

1
2
3
4
5
6
7
8
9
10
11
12
13
#include <iostream> 
#include <list> 
#include <algorithm> 

using namespace std; 

//Assuming there is a class named Something which was already declared
int main(){
list<Something> List;
list<Something>:: iterator iter; 
iter=find(List.begin(), List.end(), SomeVale);  
//....
}


Now I want to have a condition which check whether the object was found or not.
How do I implement it by using the iterator? If Ill write:

 
if(iter) do_something; 


Then the compile will generate me an error. Why would the compiler do it? Isn't the iterator is "sort of a pointer"?

Thanks!
1
2
if ( iter != List.end() ) {
}
Thanks,
but it doesn't work this way:
 
if(!iter) //.... 


?? it works with regular pointer and iterator if a-kind-of-a-pointer
AFAIK an iterator can do some similar operations as a pointer but it is NOT a pointer as such.

Following link may provide more information.
http://stackoverflow.com/questions/824157/how-to-check-whether-stl-iterator-points-at-anything
@IrarI

Thanks,
but it doesn't work this way:



if(!iter) //....



?? it works with regular pointer and iterator if a-kind-of-a-pointer


You are wrong. It does not work with regular pointers. It works only if a function is designed such a way that it returns null pointer in case it did not find the target element.
Last edited on
Topic archived. No new replies allowed.