Javatpoint Logo
Javatpoint Logo

C++ map find() function

C++ map find() function is used to find an element with the given key value k. If it finds the element then it returns an iterator pointing to the element. Otherwise, it returns an iterator pointing to the end of the map, i.e., map::end().

Syntax

Parameter

k: It specifies the key to be searched in the map container.

Return value

If it finds the element then it returns an iterator pointing to the element. Otherwise, it returns an iterator pointing to the end of the map i.e. map::end().

Example 1

Let's see a simple example to find the element with the given key value.

Output:

Iterator points to c = 300

In the above example, find() function returns the value of a given key value 'c'.

Example 2

Let's see a simple example to find the element.

Output:

Iterator points to e = 500

In the above example, find() function finds the key value e in the map m, if it is not found in the map then it returns a not found message otherwise it will display the map.

Example 3

Let's see a simple example.

Output:

Enter the element which you want to search: 4
4 found and the value is 4 = d

In the above example, find() function is used to find the element according to user?s given key value.

Example 4

Let's see a simple example.

Output:

elements in mymap:
a => 50
c => 150
d => 200
Next TopicC++ Map



Help Others, Please Share

facebook twitter pinterest