Find Items in a Container

Learn to find items in a container.

We'll cover the following

The algorithm library contains a set of functions for finding elements in a container. The std::find() function, and its derivatives, search sequentially through a container and return an iterator pointing to the first matching element, or the end() element if there's no match.

How to do it

The find() algorithm works with any container that satisfies the Forward or Input iterator iteratorqualifications. For this recipe, we'll use vector containers. The find() algorithm searches sequentially for the first matching element in a container. In this recipe, we'll walk through a few examples:

  • We'll start by declaring a vector of int in the main() function:

Get hands-on with 1200+ tech skills courses.