- C++ Library - Home
- C++ Library - <fstream>
- C++ Library - <iomanip>
- C++ Library - <ios>
- C++ Library - <iosfwd>
- C++ Library - <iostream>
- C++ Library - <istream>
- C++ Library - <ostream>
- C++ Library - <sstream>
- C++ Library - <streambuf>
- C++ Library - <atomic>
- C++ Library - <complex>
- C++ Library - <exception>
- C++ Library - <functional>
- C++ Library - <limits>
- C++ Library - <locale>
- C++ Library - <memory>
- C++ Library - <new>
- C++ Library - <numeric>
- C++ Library - <regex>
- C++ Library - <stdexcept>
- C++ Library - <string>
- C++ Library - <thread>
- C++ Library - <tuple>
- C++ Library - <typeinfo>
- C++ Library - <utility>
- C++ Library - <valarray>
- The C++ STL Library
- C++ Library - <array>
- C++ Library - <bitset>
- C++ Library - <deque>
- C++ Library - <forward_list>
- C++ Library - <list>
- C++ Library - <map>
- C++ Library - <multimap>
- C++ Library - <queue>
- C++ Library - <priority_queue>
- C++ Library - <set>
- C++ Library - <multiset >
- C++ Library - <stack>
- C++ Library - <unordered_map>
- C++ Library - <unordered_set>
- C++ Library - <vector>
- C++ Library - <algorithm>
- C++ Library - <iterator>
- The C++ Advanced Library
- C++ Library - <any>
- C++ Library - <barrier>
- C++ Library - <bit>
- C++ Library - <chrono>
- C++ Library - <cinttypes>
- C++ Library - <clocale>
- C++ Library - <condition_variable>
- C++ Library - <coroutine>
- C++ Library - <cstdlib>
- C++ Library - <cstring>
- C++ Library - <cuchar>
- C++ Library - <charconv>
- C++ Library - <cfenv>
- C++ Library - <cmath>
- C++ Library - <ccomplex>
- C++ Library - <expected>
- C++ Library - <format>
- C++ Library - <future>
- C++ Library - <flat_set>
- C++ Library - <flat_map>
- C++ Library - <filesystem>
- C++ Library - <generator>
- C++ Library - <initializer_list>
- C++ Library - <latch>
- C++ Library - <memory_resource>
- C++ Library - <mutex>
- C++ Library - <mdspan>
- C++ Library - <optional>
- C++ Library - <print>
- C++ Library - <ratio>
- C++ Library - <scoped_allocator>
- C++ Library - <semaphore>
- C++ Library - <source_location>
- C++ Library - <span>
- C++ Library - <spanstream>
- C++ Library - <stacktrace>
- C++ Library - <stop_token>
- C++ Library - <syncstream>
- C++ Library - <system_error>
- C++ Library - <string_view>
- C++ Library - <stdatomic>
- C++ Library - <variant>
- C++ STL Library Cheat Sheet
- C++ STL - Cheat Sheet
- C++ Programming Resources
- C++ Programming Tutorial
- C++ Useful Resources
- C++ Discussion
C++ unordered_multimap::find() Function
The C++ std::unordered_multimap::find() function is used to finds an element associated with key k. If operation succeeds then methods returns iterator pointing to the element otherwise it returns an iterator pointing the multimap::end().
Syntax
Following is the syntax of std::unordered_multimap::find() function.
iterator find (const key_type& k); const_iterator find (const key_type& k) const;
Parameters
- k − It indicates the key to be searched.
Return value
If the specified key is found, then an iterator pointing to the element is returned; otherwise, a multimap::end() iterator is returned.
Example 1
In the following example, we are demonstrating the usage of the unordered_multimap::find() function.
#include <iostream>
#include <unordered_map>
using namespace std;
int main(void) {
unordered_multimap<char, int> umm = {
{'a', 1},
{'b', 2},
{'c', 3},
{'d', 4},
{'c', 3},
{'d', 6},
{'e', 5}
};
auto it = umm.find('d');
cout << "Iterator points to " << it->first
<< " = " << it->second << endl;
return 0;
}
Output
Let us compile and run the above program, this will produce the following result −
Iterator points to d = 6
Example 2
Consider the following example, where we are going to find the key/value pairs whose values are even.
#include <iostream>
#include <unordered_map>
using namespace std;
int main(void) {
unordered_multimap<char, int> um = {
{'a', 1},
{'b', 2},
{'c', 3},
{'d', 4},
{'e', 5},
{'f', 6},
};
for(auto it = um.begin(); it!=um.end(); ++it){
if(it->second % 2 == 0){
it = um.find(it->first);
cout<<it->first<<" = "<<it->second<<endl;
}
}
return 0;
}
Output
If we run the above code it will generate the following output −
f = 6 d = 4 b = 2
Example 3
Let's look at the following example, where we are going to create a input string that accepts input string, if the input key is available in the multimap, then it returns their key-values otherwise not found.
#include <iostream>
#include <string>
#include <unordered_map>
using namespace std;
int main () {
unordered_multimap<string,double> umm = {
{"John",55.4},
{"Vaibhaw",65.1},
{"Sunny",50.9},
{"John",60.4}
};
string input;
cout << "who? ";
getline (cin,input);
auto got = umm.find (input);
if ( got == umm.end() )
cout << "not found";
else
cout << got->first << " is " << got->second;
return 0;
}
Output
Following is the output when our input is available in the multimap.
who? John John is 60.4
Output
Following is the output when the input is not available −
who? Aman not found
Example 4
Following is the example, where we are going to use the find() function to search an element for the specified key.
#include <iostream>
#include <string>
#include <unordered_map>
using namespace std;
int main () {
unordered_multimap<string,int> umm = {
{"John",1},
{"Vaibhaw",2},
{"Sunny",3},
{"John",4}
};
if (auto it = umm.find("John"); it != umm.end())
cout << "Found " << it->first << " " << it->second << '\n';
else
cout << "Not found\n";
return 0;
}
Output
Output of the above code is as follows −
Found John 4