C++ multiset Library - find() Function



Description

The multiset::find() function in C++ STL is used to search for an element in the multiset. It returns an iterator to the first occurrence of the element if the element is found.

Syntax

Following is the syntax of multiset::find() function −

iterator multiset_name.find(const value_type& val);
const_iterator multiset_name.find(const value_type& val) const;

Parameters

The find() function accepts one parameter that represents the value to be searched in the multiset.

Return value

The find() function returns an iterator to the first occurrence of element if found. If the element is not found, it returns an iterator pointing to multiset::end().

Exceptions

If an exception is thrown, the container remains unchanged.

Time complexity

The time complexity of multiset::find() function is O(log n), where n is the number of elements in the multiset.

Examples of multiset::find() Function

The following examples demonstrate the usage of multiset::find() function in multiset −

Finding an Element in Multiset

Below is an example to find an element in a multiset using the multiset::find() function −

#include <iostream>
#include <set>
using namespace std;
int main() {
    multiset<int> numbers = {10, 20, 30, 40, 50};
    cout << "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 multiset" << endl;
    else
        cout << "Element " << search_value
             << " not found in multiset" << endl;

    search_value = 100;
    it = numbers.find(search_value);
    if (it != numbers.end())
        cout << "Element " << search_value
             << " found in multiset" << endl;
    else
        cout << "Element " << search_value
             << " not found in multiset" << endl;

    return 0;
}

The output of the above code is given below −

Multiset: 10 20 30 40 50
Element 30 found in multiset
Element 100 not found in multiset

Finding Duplicate Elements in Multiset

You can use find() function to locate duplicate elements in a multiset. The find() function returns an iterator to the first occurrence of the element if duplicates exist.

#include <iostream>
#include <set>
using namespace std;
int main() {
    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 multiset at position: "
             << distance(numbers.begin(), it) << endl;
        cout << "Number of occurrences: "
             << numbers.count(search_value) << endl;
    } else {
       cout << "Element " << search_value
          << " not found in multiset" << endl;
    }
    return 0;
}

The output of the code is given below −

Element 30 found in multiset at position: 3
Number of occurrences: 3

Searching a Pair using find()

In this example, we are searching for a key value pair in multiset using the multiset::find() function. Here, lexicographical comparison is used, i.e., first it will compare first value, then the second value.

#include <iostream>
#include <set>
using namespace std;

int main() {
    multiset<pair<int, string>> data = {{1, "A"}, {2, "B"}, 
                                        {1, "C"}};
    cout << "Multiset elements:\n";
    for (const auto &p : data)
        cout << "(" << p.first << ", " << p.second << ")\n";
    cout << endl;

    // find() function to search for the pair
    pair<int, string> searchPair = {1, "C"};
    auto it = data.find(searchPair);

    cout << "Searching for pair: (" << searchPair.first 
         << ", " << searchPair.second << ")\n";

    if (it != data.end())
        cout << "Found pair: (" << it->first << ", " 
             << it->second << ")\n";
    else
        cout << "Pair not found\n";

    return 0;
}

The output of the above code is given below −

Multiset elements:
(1, A)
(1, C)
(2, B)

Searching for pair: (1, C)
Found pair: (1, C)
multiset.htm
Advertisements