424,987 Members | 971 Online
Need help? Post your question and get tips & solutions from a community of 424,987 IT Pros & Developers. It's quick & easy.

# About the find() is STL algorithm

 P: n/a Hi All, Just a thought, about the find() algorithm in the C++ STL. I read that the find algorithm can take a range of iterators. If it does not find the element it is looking for in that range it returns the iterator to the last element in the range, not to the last element in the container, to the last element in the range. That being said, how can we tell if find() has been successful in finding the element we need? Its easy when we are searching the whole container, we can always the container's end() method, but when we are searching only a subset of all the elements in a container, what then? Do we have a better way than checking the element that find returns again?(sounds inefficient, repetitive). Would appreciate your thoughts on this matter. Thanks, Draw Jun 10 '06 #1
5 Replies

 P: n/a * Draw: Just a thought, about the find() algorithm in the C++ STL. I read that the find algorithm can take a range of iterators. If it does not find the element it is looking for in that range it returns the iterator to the last element in the range, not to the last element in the container, to the last element in the range. A range of elements is specified as an iterator A to the first element, and an iterator B one past the last element. If std::find fails it returns B, in all cases. Compare the result iterator to B. -- A: Because it messes up the order in which people normally read text. Q: Why is it such a bad thing? A: Top-posting. Q: What is the most annoying thing on usenet and in e-mail? Jun 10 '06 #2

 P: n/a Alf P. Steinbach wrote: * Draw: Just a thought, about the find() algorithm in the C++ STL. I read that the find algorithm can take a range of iterators. If it does not find the element it is looking for in that range it returns the iterator to the last element in the range, not to the last element in the container, to the last element in the range. A range of elements is specified as an iterator A to the first element, and an iterator B one past the last element. If std::find fails it returns B, in all cases. Compare the result iterator to B. -- Thanks, but I don't think I got an answer for my question. Are you saying that I have to always give the start and end as the range? What if I don't want to, what if I want to search only the first 10 elements of the vector? I went ahead and tried to do that, the code is right after I stop blabbering :-), and find() does indeed return the last element of the range if it doesn't find anything(not that of the container), but what if the last element of the range does satisfy the condition find() is looking for, in that case, it would still return the last element in the range. Thats ambiguous, isn't it? And what you say above proves it, it does return the last element in the range you provide, but you are able to work around the situation I mention because your last element happens to equal the iterator *.end()(indeed, one past the last element). //[START] #include #include #include int main(void){ unsigned int index; vector intvec(20,3); vector::iterator it,ret; for(it = intvec.begin(),index=0;it != intvec.end();++it,++index){ *it =+ index; cout << "vector Element value equated to subscript "<<*it<

 P: n/a Draw wrote: Alf P. Steinbach wrote: * Draw: Just a thought, about the find() algorithm in the C++ STL. I read that the find algorithm can take a range of iterators. If it does not find the element it is looking for in that range it returns the iterator to the last element in the range, not to the last element in the container, to the last element in the range. A range of elements is specified as an iterator A to the first element, and an iterator B one past the last element. If std::find fails it returns B, in all cases. Compare the result iterator to B. -- Thanks, but I don't think I got an answer for my question. Are you saying that I have to always give the start and end as the range? What if I don't want to, what if I want to search only the first 10 elements of the vector? I went ahead and tried to do that, the code is right after I stop blabbering :-), and find() does indeed return the last element of the range if it doesn't find anything(not that of the container), but what if the last element of the range does satisfy the condition find() is looking for, in that case, it would still return the last element in the range. Thats ambiguous, isn't it? The "one past the last element" concept is what you seem to be missing. The range is specified by giving an iterator A to the first element and an iterator B to _one past_ the last element in the range i.e. iterator B itself does not point to an element within the range. Iterator B will therefore never be returned if the element is found. Jun 11 '06 #4

 P: n/a Markus Schoder wrote: Draw wrote: Alf P. Steinbach wrote: * Draw: > > Just a thought, about the find() algorithm in the C++ STL. I read that > the find algorithm can take a range of iterators. If it does not find > the element it is looking for in that range it returns the iterator to > the last element in the range, not to the last element in the > container, to the last element in the range. A range of elements is specified as an iterator A to the first element, and an iterator B one past the last element. If std::find fails it returns B, in all cases. Compare the result iterator to B. -- Thanks, but I don't think I got an answer for my question. Are you saying that I have to always give the start and end as the range? What if I don't want to, what if I want to search only the first 10 elements of the vector? I went ahead and tried to do that, the code is right after I stop blabbering :-), and find() does indeed return the last element of the range if it doesn't find anything(not that of the container), but what if the last element of the range does satisfy the condition find() is looking for, in that case, it would still return the last element in the range. Thats ambiguous, isn't it? The "one past the last element" concept is what you seem to be missing. The range is specified by giving an iterator A to the first element and an iterator B to _one past_ the last element in the range i.e. iterator B itself does not point to an element within the range. Iterator B will therefore never be returned if the element is found. Thanks Guys! Now I see. Thanks for clarifying that. Jun 11 '06 #5

 P: n/a Draw wrote: Alf P. Steinbach wrote:* Draw:Just a thought, about the find() algorithm in the C++ STL. I read thatthe find algorithm can take a range of iterators. If it does not findthe element it is looking for in that range it returns the iterator tothe last element in the range, not to the last element in thecontainer, to the last element in the range.A range of elements is specified as an iterator A to the first element,and an iterator B one past the last element.If std::find fails it returns B, in all cases.Compare the result iterator to B.-- Thanks, but I don't think I got an answer for my question. Are you saying that I have to always give the start and end as the range? What if I don't want to, what if I want to search only the first 10 elements of the vector? I went ahead and tried to do that, the code is right after I stop blabbering :-), and find() does indeed return the last element of the range if it doesn't find anything(not that of the container), but what if the last element of the range does satisfy the condition find() is looking for, in that case, it would still return the last element in the range. Thats ambiguous, isn't it? And what you say above proves it, it does return the last element in the range you provide, but you are able to work around the situation I mention because your last element happens to equal the iterator *.end()(indeed, one past the last element). See the changed code. //[START] #include #include #include int main(void){ unsigned int index; vector intvec(20,3); vector::iterator it,ret; for(it = intvec.begin(),index=0;it != intvec.end();++it,++index){ *it =+ index; cout << "vector Element value equated to subscript "<<*it<

### This discussion thread is closed

Replies have been disabled for this discussion.