- 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 - <unordered_multiset>
- 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_multiset::find() Function
Description
The unordered_multiset::find() function in C++ STL is used to search for an element in the unordered_multiset. It uses hash value for searching the element in the container. It returns an iterator to one of the occurrences of the element if the element is found.
Syntax
Following is the syntax of unordered_multiset::find() function −
iterator unordered_multiset_name.find(const value_type& val); const_iterator unordered_multiset_name.find(const value_type& val) const;
Parameters
The find() function accepts one parameter that represents the value to be searched in the unordered_multiset.
Return value
The find() function returns an iterator to one of the occurrences of element if found. If the element is not found, it returns an iterator pointing to unordered_multiset::end().
Exceptions
If an exception is thrown, the container remains unchanged.
Time complexity
The time complexity of unordered_multiset::find() function is O(1) on average and O(n) in worst case, where n is the number of elements in the unordered_multiset.
Examples of unordered_multiset::find() Function
The following examples demonstrate the usage of unordered_multiset::find() function in unordered_multiset −
Finding an Element in Unordered_multiset
Below is an example to find an element in an unordered_multiset using the unordered_multiset::find() function −
#include <iostream>
#include <unordered_set>
using namespace std;
int main() {
unordered_multiset<int> numbers = {10, 20, 30, 40, 50};
cout << "Unordered_multiset:";
for (const auto &num : numbers)
cout << " " << num;
cout << endl;
int search_value = 30;
auto it = numbers.find(search_value);
if (it != numbers.end())
cout << "Element " << search_value
<< " found in unordered_multiset" << endl;
else
cout << "Element " << search_value
<< " not found in unordered_multiset" << endl;
search_value = 100;
it = numbers.find(search_value);
if (it != numbers.end())
cout << "Element " << search_value
<< " found in unordered_multiset" << endl;
else
cout << "Element " << search_value
<< " not found in unordered_multiset" << endl;
return 0;
}
The output of the above code is given below −
Unordered_multiset: 50 40 30 20 10 Element 30 found in unordered_multiset Element 100 not found in unordered_multiset
Finding Duplicate Elements in Unordered_multiset
You can use find() function to locate duplicate elements in an unordered_multiset. The find() function returns an iterator to one of the occurrences of the element if duplicates exist.
#include <iostream>
#include <unordered_set>
using namespace std;
int main() {
unordered_multiset<int> numbers = {40, 10, 20, 20, 30, 30, 30, 40};
int search_value = 30;
auto it = numbers.find(search_value);
if (it != numbers.end()) {
cout << "Element " << search_value
<< " found in unordered_multiset" << endl;
cout << "Number of occurrences: "
<< numbers.count(search_value) << endl;
} else {
cout << "Element " << search_value
<< " not found in unordered_multiset" << endl;
}
return 0;
}
The output of the code is given below −
Element 30 found in unordered_multiset Number of occurrences: 3
Searching a String using find()
In this example, we are searching for a string in unordered_multiset using the unordered_multiset::find() function. This demonstrates how find() works with string elements in an unordered_multiset.
#include <iostream>
#include <unordered_set>
#include <string>
using namespace std;
int main() {
unordered_multiset<string> data = {"Apple", "Banana",
"Apple", "Cherry"};
cout << "Unordered_multiset elements:\n";
for (const auto &s : data)
cout << s << "\n";
cout << endl;
// find() function to search for the string
string searchStr = "Apple";
auto it = data.find(searchStr);
cout << "Searching for string: " << searchStr << "\n";
if (it != data.end())
cout << "Found string: " << *it << "\n";
else
cout << "String not found\n";
return 0;
}
The output of the above code is given below −
Unordered_multiset elements: Cherry Apple Apple Banana Searching for string: Apple Found string: Apple
Difference Between multiset::find and unordered_multiset::find
The differences between multiset::find and unordered_multiset::find functions are given below −
| multiset::find | unordered_multiset::find |
|---|---|
| The multiset::find() function works on a multiset. | The unordered_multiset::find() function works on an unordered multiset. |
| It uses a tree-based lookup using comparisons for searching the key. | It calculates hash value and then scans its corresponding hash bucket for searching the key. |
| The time complexity is O(log n). | The time complexity is O(1) in average time and O(n) in the worst case. |
| In case of duplicate values, it returns an iterator to the first occurrence in sorted order. | In case of duplicate values, it returns an iterator to one of the matching elements. |