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.
- searching in fork()
- Pattern Searching using C++ library
- C++ Mathematical Functions
- Pure Functions
- Inline Functions in C++
- Macros vs Functions
- List in C++ | Set 2 (Some Useful Functions)
- Functions that cannot be overloaded in C++
- erf | Error functions using cmath in C++
- fill() and fill_n() functions in C++ STL
- Return from void functions in C++
- Const member functions in C++
- How arrays are passed to functions in C/C++
- Ceil and Floor functions in C++
- Builtin functions of GCC compiler