Introduction
In this lab, you will learn how to use the find()
method in a Map in C++. The find()
method is used to search for an element in a Map with a given key. This lab assumes you have basic knowledge of C++ and the Map container.
In this lab, you will learn how to use the find()
method in a Map in C++. The find()
method is used to search for an element in a Map with a given key. This lab assumes you have basic knowledge of C++ and the Map container.
First, you need to create a new C++ file named main.cpp
in the ~/project
directory. You can use any text editor or IDE to create and edit this file.
cd ~/project
touch main.cpp
In the top of the main.cpp
file, include the necessary libraries and using directive.
#include <iostream>
#include <map> //include map library
using namespace std;
Define the main()
function to start writing your code.
int main() {
//Your code
return 0; //Exit program
}
Create a Map container with integer keys and values. The Map container will store key-value pairs sorted in ascending order of keys.
map<int, int> myMap; //Create a Map container with integer keys and values
Use the insert()
method to insert key-value pairs into the Map container. This step will insert five elements into the Map.
myMap.insert(make_pair(3, 9));
myMap.insert(make_pair(2, 4));
myMap.insert(make_pair(5, 25));
myMap.insert(make_pair(9, 81));
myMap.insert(make_pair(1, 1));
Use the find(x)
method to search for an element with key x in the Map. If the element is found, the method returns an iterator to the element. If not found, the method returns an iterator to the end()
of the Map container. Here, we will search for elements with keys 5 and 6.
map<int, int>::iterator it; //Create an iterator for the Map container
it = myMap.find(5); //Find the element with key 5 in the Map
if (it != myMap.end()) {
cout << "Element with key 5 is found and the value is " << it->second << endl;
} else {
cout << "Element with key 5 is not found\n";
}
it = myMap.find(6); //Find the element with key 6 in the Map
if (it != myMap.end()) {
cout << "Element with key 6 is found and the value is " << it->second << endl;
} else {
cout << "Element with key 6 is not found\n";
}
Compile the main.cpp
file using the following command in the terminal:
g++ main.cpp -o main
Run the program using the following command:
./main
After running the program, you should see the following output in the terminal:
Element with key 5 is found and the value is 25
Element with key 6 is not found
This output shows that the element with key 5 is found in the Map and its value is 25. The element with key 6 is not found in the Map.
Copy and paste the following complete code into the main.cpp
file.
#include <iostream>
#include <map>
using namespace std;
int main() {
map<int, int> myMap; //Create a Map container with integer keys and values
myMap.insert(make_pair(3, 9));
myMap.insert(make_pair(2, 4));
myMap.insert(make_pair(5, 25));
myMap.insert(make_pair(9, 81));
myMap.insert(make_pair(1, 1));
map<int, int>::iterator it; //Create an iterator for the Map container
it = myMap.find(5); //Find the element with key 5 in the Map
if (it != myMap.end()) {
cout << "Element with key 5 is found and the value is " << it->second << endl;
} else {
cout << "Element with key 5 is not found\n";
}
it = myMap.find(6); //Find the element with key 6 in the Map
if (it != myMap.end()) {
cout << "Element with key 6 is found and the value is " << it->second << endl;
} else {
cout << "Element with key 6 is not found\n";
}
return 0; //Exit program
}
Congratulations! You have successfully learned how to use the find()
method in a Map container in C++. The find()
method is useful for searching for elements with a specific key in a Map container. Remember that the find()
method returns an iterator to the element if the element is found, and an iterator to the end()
of the Map container if the element is not found.