Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

some containers in stl don't have find function

Tags:

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.

like image 795
Hanna Khalil Avatar asked Sep 02 '14 07:09

Hanna Khalil


People also ask

How many containers are there in STL?

In C++, there are generally 3 kinds of STL containers: Sequential Containers. Associative Containers. Unordered Associative Containers.

What does find in STL return?

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.


1 Answers

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.

like image 173
juanchopanza Avatar answered Sep 18 '22 13:09

juanchopanza