C++ Programming Code Examples
Learn C++ Language
Map Library find() Function in C++ Programming Language
Map Library find() Function in C++
Get iterator to element. Searches the container for an element with a key equivalent to k and returns an iterator to it if found, otherwise it returns an iterator to map::end. Two keys are considered equivalent if the container's comparison object returns false reflexively (i.e., no matter the order in which the elements are passed as arguments). Another member function, map::count, can be used to just check whether a particular key exists.
Syntax for Map find() Function in C++
#include <map>
iterator find (const key_type& k);
const_iterator find (const key_type& k) const;
k
Key to be searched for. Member type key_type is the type of the keys for the elements in the container, defined in map as an alias of its first template parameter (Key).
The function accepts one mandatory parameter key which specifies the key to be searched in the map container.
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().
Function returns an iterator to the element, if an element with specified key is found, or map::end otherwise.
If the map object is const-qualified, the function returns a const_iterator. Otherwise, it returns an iterator.
Member types iterator and const_iterator are bidirectional iterator types pointing to elements (of type value_type).
Notice that value_type in map containers is an alias of pair<const key_type, mapped_type>.
Complexity
Logarithmic in size
Iterator validity
No changes
Data races
The container is accessed (neither the const nor the non-const versions modify the container). No mapped values are accessed: concurrently accessing or modifying elements is safe.
Exception safety
Strong guarantee: if an exception is thrown, there are no changes in the container.
/* returns an iterator or a constant iterator that refers to the position where the key is present in the map by map::find() function code example. */
// C++ program for illustration of map::find() function
#include <bits/stdc++.h>
using namespace std;
int main()
{
// Initialize container
map<int, int> mp;
// Insert elements in random order
mp.insert({ 2, 30 });
mp.insert({ 1, 40 });
mp.insert({ 3, 20 });
mp.insert({ 4, 50 });
cout << "Elements from position of 3 in the map are : \n";
cout << "KEY\tELEMENT\n";
// find() function finds the position
// at which 3 is present
for (auto itr = mp.find(3); itr != mp.end(); itr++) {
cout << itr->first << '\t' << itr->second << '\n';
}
return 0;
}
Using Templates so that any type of data can be stored in stack without multiple defination of class. Contains "Location of Topmost data" pushed onto stack. Sets the top location to -1
In C++ language, Multilevel Inheritance The class A serves as a base class for the derived class B, which in turn serves as a 'base class' for the derived class C. The class B is known