Sign in to follow this  

can someone explain to me std::find?

This topic is 3932 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

according to the STL pocket reference:
Quote:
Inp find(Inp first, Inp last, const T& value) finds the first occurence of value in the range [first, last] and returns an iterator that points to the value. if value is not found, last is returned
I don't get this. Presumably you're looking in a container for the first thing between these two iterators. If you've got an iterator to the first, and an iterator to the last, then by calling find, aren't you looking for the FIRST value between those two? i.e.: either first itself or the second one. What's the point of this? Can't you just get the second object in the list yourself? Its just that whenever I see this code being used (so far) they always enter container.first() & container.end() into the first & last values. I really don't get the point, maybe someone can explain this to me?

Share this post


Link to post
Share on other sites
Quote:
Original post by EGD Eric
If you've got an iterator to the first, and an iterator to the last, then by calling find, aren't you looking for the FIRST value between those two? i.e.: either first itself or the second one.


You have an iterator to the first object in a sequence of objects, and an iterator which is right after the last object of that same sequence. The sequence may well contain more that one object: in fact, a lot of objects will be present between the first object and the last object.

The std::find function will return an iterator to the first object in the sequence that is equal to its third argument (or the past-the-end iterator if none is found). So, if the first object in the sequence is different from the third argument, but the second object works, std::find will return an iterator that is neither the first nor the last.

Share this post


Link to post
Share on other sites

This topic is 3932 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