Usually, main purpose of using map stl container is for efficient search operations and sorted order retrieval. As map stores key-value pair, all the search operations take “O(log(n))” time (n is size of map). Different types of search functions exists in C++ language, each having different functions. In the context of competitive programming, this turns out to be useful when search operations are required and performs better than other containers. Some search operations are discussed below.
find() is used to search for the key-value pair and accepts the “key” in its argument to find it. This function returns the pointer to the element if the element is found, else it returns the pointer pointing to the last position of map i.e “map.end()” .
Key-value pair present : b->10 Key-value pair not present in map
lower_bound() is also used for the search operation but sometimes also returns a valid key-value pair even if it is not present in map . lower_bound() returns address of key value pair, if one is present in map, else returns the address to the smallest key greater than key mentioned in its arguments. If all keys are smaller than the key to be found, it points to “map.end()” .
Key-value pair returned : b->10 Key-value pair returned : h->20 Key-value pair not present in map
upper_bound() is also used for the search operation and never returns the key-value pair searched for . upper_bound() returns address of key value pair coming exactly next to the searched key, if one is present in map. If all keys are smaller than the key to be found, it points to “map.end()”
Key-value pair returned : c->15 Key-value pair returned : h->20 Key-value pair not present in map
Yet another function to search in map, it returns the range containing the searched key. As map contains unique elements, range returned contains at most 1 element. This function returns a iterator of pair, whose 1st element points to lower_bound() of the searched key pair, and second element points to the upper_bound() of the searched key. If key is not present, both first element and second element point to the next greater element.
The lower_bound of key is : b->10 The upper_bound of key is : c->15 The lower_bound of key is : h->20 The upper_bound of key is : h->20
This article is contributed by Manjeet Singh. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
- map::at() and map::swap() in C++ STL
- Pattern Searching using C++ library
- searching in fork()
- Minimum and Maximum of all subarrays of size K using Map
- Find whether an array is subset of another array using Map
- Implementing Counting Sort using map in C++
- Program to find frequency of each element in a vector using map in C++
- erf | Error functions using cmath in C++
- Computing index using pointers returned by STL functions in C++
- Find and print duplicate words in std::vector<string> using STL functions
- Code to generate the map of India (with explanation)
- Descending order in Map and Multimap of C++ STL
- Map in C++ Standard Template Library (STL)
- Inserting elements in std::map (insert, emplace and operator )
- Different ways to delete elements in std::map (erase() and clear())
- Hash Table vs STL Map
- Map of pairs in STL
- Traversing a map (or unordered_map) in C++ STL
- Operator overloading in C++ to print contents of vector, map, pair, ..
- Check if a key is present in a C++ map or unordered_map