can someone explain to me std::find?

Started by
3 comments, last by EGD Eric 17 years, 1 month ago
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?
Advertisement
You need do give it an iterator to a starting and ending location, however the third argument is the value you are trying to find in the container.

I think the example on this site helps explain it:
http://www.msoe.edu/eecs/ce/courseinfo/stl/find.htm

Hope it helps,
Chryzmo
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.
Moved to For Beginners.
that's a good explanation guys, thanks!

This topic is closed to new replies.

Advertisement