Studytonight is now part of the GUVI universe. Explore GUVI →
🤩 New Cool Developer Tools for you. Explore →
FREE JavaScript Video Series Start Learning →
Signup/Sign In
Tests
MCQs to test your knowledge.
Compilers
Compilers to execute code in browser.
Index
LAST UPDATED: NOVEMBER 1, 2020

C++ Using find() method in STL Map Program

Hello Everyone!

In this tutorial, we will learn about the working of find() method in a Map in the C++ programming language.

To understand the basic functionality of the Map Container in STL, we will recommend you to visit the C++ STL Map Container, where we have explained this concept in detail from scratch.

The find(x) method returns an iterator to the pair with key as x and points to end() if not found.

For a better understanding of its implementation, refer to the well-commented C++ code given below.

Code:

#include <iostream>
#include <bits/stdc++.h>
using namespace std;
int main()
{
 cout << "\n\nWelcome to Studytonight :-)\n\n\n";
 cout << " ===== Program to demonstrate the working of find() method in a Map, in CPP ===== \n\n\n";
 cout << "*** The find(x) method returns an iterator to the pair with key as x and points to end() if not found. *** \n\n";
 //Map declaration (Map with key and value both as integers)
 map<int, int> m;
 //Filling the elements by using the insert() method.
 cout << "\n\nFilling the Map with key-value pairs of integers in random order."; //Map automatically stores them in increasing order of keys
 //make_pair() is used to insert a key value pair into the map
 m.insert(make_pair(3, 9));
 m.insert(make_pair(2, 4));
 m.insert(make_pair(5, 25));
 m.insert(make_pair(9, 81));
 m.insert(make_pair(1, 1));
 cout << "\n\nThe number of elements in the Map are: " << m.size();
 cout << "\n\nThe elements of the Map m are: ";
 map<int, int>::iterator i;
 int j = 0;
 for (i = m.begin(); i != m.end(); i++)
 {
 cout << "\n The Key " << ++j << " of Map m is " << i->first << " and it's corresponding value is " << i->second;
 }
 //Finding the map element with key 5
 i = m.find(5);
 if (i != m.end())
 {
 cout << "\n\nThe Map element with key 5 is " << i->second;
 }
 else
 {
 cout << "\n\nThe Map element with key 5 does not exist.";
 }
 i = m.find(6);
 if (i != m.end())
 {
 cout << "\n\nThe Map element with key 6 is " << i->second;
 }
 else
 {
 cout << "\n\nThe Map element with key 6 does not exist.";
 }
 cout << "\n\n\n";
 return 0;
}

Output:

C++ find() map

We hope that this post helped you develop a better understanding of the concept of the find() method in Map and its implementation in C++. For any query, feel free to reach out to us via the comments section down below.

Keep Learning : )



About the author:
Nikita Pandey is a talented author and expert in programming languages such as C, C++, and Java. Her writing is informative, engaging, and offers practical insights and tips for programmers at all levels.

Learn to Code
Learn and practice coding side-by-side.
NEW
C language Course
115+ coding exercises
Javascript Course
85+ coding exercises

AltStyle によって変換されたページ (->オリジナル) /