C++ multiset Library - contains() Function



Description

The multiset::contains() function in C++ STL was introduced in C++20. It checks if an element exists in the multiset container or not. It searches for a key and returns a boolean value that represents whether the element is present or not in the multiset.

Syntax

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

// Check by key
bool contains(const key_type& key) const;
// Check by key-like object
template< class K >
bool contains(const K& keyObj) const;

Parameters

The contains() function accepts a single parameter. It can either be a key or a key-like object that you want to search in the multiset.

Return value

The contains() function returns a boolean value, i.e., true if element found and false if not found.

Exceptions

If an exception is thrown, the container remains unchanged.

Time complexity

The time complexity of multiset::contains() function is O(log N), where N is the size of the multiset.

Examples of multiset::contains() Function

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

Checking if Element Exists in Multiset

Below is an example to check if an element (30) exists in a multiset nums using the multiset::contains() function −

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

int main() {
    multiset<int> nums = {10, 20, 30, 40, 50};
    cout << "Multiset elements:";
    for (const auto &num : nums)
        cout << " " << num;
    cout << endl;
    if (nums.contains(30)){
        cout << "Element 30 is present in the multiset" << endl;
    } else {
        cout << "Element 30 is not present in the multiset" << endl;
    }
    if (nums.contains(60)){
        cout << "Element 60 is present in the multiset" << endl;
    } else {
        cout << "Element 60 is not present in the multiset" << endl;
    }
    return 0;
}

The output of the above code is given below −

Multiset elements: 10 20 30 40 50
Element 30 is present in the multiset 
Element 60 is not present in the multiset

Checking Duplicate Elements

In this example, we have used the multiset::contains() function to check if an element (20) exists in the multiset nums that contains duplicate values.

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

int main() {
    multiset<int> nums = {10, 20, 20, 20, 30, 40};
    cout << "Multiset elements:";
    for (const auto &num : nums)
        cout << " " << num;
    cout << endl;
    if (nums.contains(20)){
        cout << "Element 20 is present in the multiset" << endl;
        cout << "Count of element 20: " << nums.count(20) << endl;
    } else{
        cout << "Element 20 is not present in the multiset" << endl;
    }
    return 0;
}

The output of the above code is given below −

Multiset elements: 10 20 20 20 30 40
Element 20 is present in the multiset
Count of element 20: 3

Using contains() with String Multiset

Here is an example to check the existence of string elements in a multiset words using the multiset::contains() function

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

int main() {
    multiset<string> words = {"apple", "banana", "cherry", "banana"};
    cout << "Multiset elements:";
    for (const auto &word : words)
        cout << " " << word;
    cout << endl;
    string search1 = "banana";
    string search2 = "mango";
    if (words.contains(search1)){
        cout << "'" << search1 << "' is present in the multiset" << endl;
    } else {
        cout << "'" << search1 << "' is not present in the multiset" << endl;
    }
    if (words.contains(search2)){
        cout << "'" << search2 << "' is present in the multiset" << endl;
    } else {
        cout << "'" << search2 << "' is not present in the multiset" << endl;
    }
    return 0;
}

The output of the above code is given below −

Multiset elements: apple banana banana cherry
'banana' is present in the multiset
'mango' is not present in the multiset

Difference Between contains() and find() Function

The key differences between the contains() and find() functions in C++ STL multiset is given below −

contains() find()
It returns a boolean value. It returns an iterator that points to element if the element is found.
You can only check if the element exists in the multiset or not. You can use the iterator to access or erase the element.
It was introduced in C++20. It was introduced in C++98.
multiset.htm
Advertisements