std::find() question.


R. Clayton (rclayton@monmouth.edu)
(no date)


I want to use find() and find_if() in containers holding values of my own data
type. While searching in Internet I came across a page that said I can
overload find() for user-defined data types. Is there a simpler way to use
find() with my own data types?

  Yes. std::find(s, e, v) searches the iterator range (s, e) for the first
  (leftmost, closest to s) value equal to v and returns an iterator to it (or
  it returns e if there's no such value). find() uses == to test for equality,
  so if the iterator range contains values your own class, you have to make
  sure that operator == is defined for your class. It is not necessary for you
  to rewrite find(), just make sure operator == is defined for your class.

  A similar situation holds for std::find_if(s, e, p), which searches the
  iterator range (s, e) for the first value v such that p(v) returns true. In
  this case you have to write the predicate p(), although the STL has several
  standard predicates you can use if you want.



This archive was generated by hypermail 2.0b3 on Thu Dec 18 2003 - 16:15:05 EST