Sign in to follow this  

Question about iterators.

This topic is 4596 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I have made a function that searches through a vector for a specific item and then returns an iterator "pointing" to that item. The problem is that if nothing was found I need it to return NULL or something that tells the caller that the search was unsuccessful. I tried using NULL but aparently iterators can't be nulled? I thought about making a structure with the iterator and a bool that tells whether or not it was successful but there has to be an easier way... right?

Share this post


Link to post
Share on other sites
In general, when something is not found in the vector/list/map/whatever your iterator will be pointing to YourList.end(). So you simply check to see if the returned iterator == YourList.end(), and there you have it.

Share this post


Link to post
Share on other sites
Exactly. Look at std::find() located in <algorithm> for an example of exactly what you're doing. In fact, I'd recommend you use this function instead.

Share this post


Link to post
Share on other sites

This topic is 4596 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this