Some of the STL containers such as std::list
and std::vector
don't have find()
method as a member function. Why is that? I know that there is the alternative of using std::find
from <algorithm>
but still this use isn't 100% natural.
In C++, there are generally 3 kinds of STL containers: Sequential Containers. Associative Containers. Unordered Associative Containers.
find() returns an iterator which points to the position of the element which is searched. If the element is not present in the set, then it returns the element just after the last element of the set container.
The general design principle is to use std::find
where possible, and implement find
member functions when it is more efficient.
The containers that do have a find
member are containers which have a more efficient element look-up mechanism then the linear search performed in std::find
. For example, binary search trees such as std::set
and std::map
, or hash tables such as their unordered
counterparts.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With