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.
Advertisements