The map::find() is a built-in function in C++ STL which returns an iterator or a constant iterator that refers to the position where the key is present in the map. If the key is not present in the map container, it returns an iterator or a constant iterator which refers to map.end().
iterator map_name.find(key) or constant iterator map_name.find(key)
Parameters: The function accepts one mandatory parameter key which specifies the key to be searched in the map container.
Return Value: The function returns an iterator or a constant iterator which refers to the position where the key is present in the map. If the key is not present in the map container, it returns an iterator or a constant iterator which refers to map.end().
Time Complexity for Searching element :
Time complexity for searching elements in std::map is O(log n). Even in worst case it will be O(log n) because elements are stored internally as Balanced Binary Search tree (BST) whereas, in std::unordered_map best case time complexity for searching is O(1).
Below is the illustration of above function:
The elements from position 3 in map are : KEY ELEMENT 3 20 4 50
Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: DSA Self Paced. Become industry ready at a student-friendly price.
- set find() function in C++ STL
- multiset find() function in C++ STL
- unordered_multimap find() function in C++ STL
- unordered_set find() function in C++ STL
- unordered_multiset find() function in C++STL
- Difference between user defined function and library function in C/C++
- Function Overloading vs Function Overriding in C++
- How to call some function before main() function in C++?
- How to call function within function in C or C++
- Difference between Virtual function and Pure virtual function in C++
- What happens when a virtual function is called inside a non-virtual function in C++
- std::find in C++
- unordered_map find in C++ STL
- Find if there is a subarray with 0 sum
- multimap find() in C++ STL
- string find in C++
- How to find the Entry with largest Value in a C++ Map
- Find elements of an Array which are Odd and Even using STL in C++
- How to find the sum of elements of an Array using STL in C++?
- How to find the sum of elements of a Vector using STL in C++?
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.