Jump to content
  • Advertisement
Sign in to follow this  
Broccoli

Question about iterators.

This topic is 4901 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
Advertisement
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
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!