map find() function in C++
The C++ Standard Library's std::map class contains a member method called find(). It is used to look for a map element with the specified key value of k. If the element is present in the map, the method returns an iterator pointing to it. If not, it produces an iterator pointing to map::end(), which denotes the map's end.
Syntax:
iterator find(const Key& key);
or
constant iterator=map_name.find(key)
Iterator refers to the sort of iterator that the std::map utilizes, and the key denotes the sort of key you want to search for. The key you want to search for must be sent as an argument to the find() function on a std::map object in order to utilize it. The method will either return the end iterator or an iterator pointing to the element that was found if the key is absent from the map.
Example: 1
#includeusing namespace std; int main() { // Initialize the map container map myMap; // Insert elements in arbitrary order myMap.insert({2, 30}); myMap.insert({1, 40}); myMap.insert({3, 20}); myMap.insert({4, 50}); int key1 = 2; // Key 1 to find (exists in the map) int key2 = 5; // Key 2 to find (does not exist in the map) cout << "Searching for key " << key1 << "... "; if (myMap.find(key1) != myMap.end()) { // If the key is found before the end of the map cout << "Found! Value: " << myMap[key1] << endl; // If the key is present, you can access its associated value using the index operator } else { cout << "Not found!" << endl; } cout << "Searching for key " << key2 << "... "; if (myMap.find(key2) != myMap.end()) { // If the key is found before the end of the map cout << "Found! Value: " << myMap[key2] << endl; // If the key is present, you can access its associated value using the index operator } else { cout << "Not found!" << endl; } return 0; }
Output
Element 2: Found! Value: 30 Element 5: Not found
Explanation: Map myMap; is used to initialize the map container. It is a key-value container that arranges the items according to the keys. The map is updated with key-value pairs by using the myMap.insert("key, value"). In this scenario, four pairs are added, the numbers 2, 1, 3, and 4, represented as keys and values that correspond to 30, 40, 20, and 50.
The element1 is assigned with 2 and element2 is assigned with 5. The map will be searched for these items. For element1, the initial search is carried out. It determines if myMap.find(element1) and myMap.end() are not the same. When an element is found, the find() function produces an iterator pointing to it; otherwise, it returns myMap.end().
Example: 2
#include#include
Output:
Quantity found! Fruit: Banana, Quantity: 20 Quantity not found!
Explanation:
In the following example, we construct a fruitMap std::map that converts texts to numbers. Using fruitMap, we add four key-value pairs to the fruitMap.insert(). The element with the key "Banana" is then searched for in the map using fruitMap.find("Banana"). When an element is discovered, the find() function produces an iterator pointing to it. Otherwise, it returns fruitMap.end().
To determine if the element was located, we compare the iterator with fruitMap.end(). We report the detected element's key and value if it is not equal to fruitMap.end(). If the element isn't discovered, we print a message to that effect.