- 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::contains() Function
Description
The unordered_multiset::contains() function in C++ STL was introduced in C++20. It checks if an element exists in the unordered_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 unordered_multiset.
Syntax
Following is the syntax of unordered_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 unordered_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 unordered_multiset::contains() function is O(1) on average, and in the worst case, it can be O(N) due to frequent hash collisions, where N is the size of the unordered_multiset.
Examples of unordered_multiset::contains() Function
The following examples demonstrate the usage of unordered_multiset::contains() function in unordered_multiset −
Checking if Element Exists in Unordered_multiset
Below is an example to check if an element (30) exists in an unordered_multiset nums using the unordered_multiset::contains() function −
#include <iostream>
#include <unordered_set>
using namespace std;
int main() {
unordered_multiset<int> nums = {10, 20, 30, 40, 50};
cout << "Unordered_multiset elements:";
for (const auto &num : nums)
cout << " " << num;
cout << endl;
if (nums.contains(30))
cout << "Element 30 is present in the unordered_multiset" << endl;
else
cout << "Element 30 is not present in the unordered_multiset" << endl;
if (nums.contains(60))
cout << "Element 60 is present in the unordered_multiset" << endl;
else
cout << "Element 60 is not present in the unordered_multiset" << endl;
return 0;
}
The output of the above code is given below −
Unordered_multiset elements: 50 40 30 20 10 Element 30 is present in the unordered_multiset Element 60 is not present in the unordered_multiset
Checking Duplicate Elements
In this example, we have used the unordered_multiset::contains() function to check if an element (20) exists in the unordered_multiset nums that contains duplicate values.
#include <iostream>
#include <unordered_set>
using namespace std;
int main(){
unordered_multiset<int> nums = {10, 20, 20, 20, 30, 40};
cout << "Unordered_multiset elements:";
for (const auto &num : nums)
cout << " " << num;
cout << endl;
if (nums.contains(20)){
cout << "Element 20 is present in the unordered_multiset" << endl;
cout << "Count of element 20: " << nums.count(20) << endl;
} else {
cout << "Element 20 is not present in the unordered_multiset" << endl;
}
return 0;
}
The output of the above code is given below −
Unordered_multiset elements: 40 30 20 20 20 10 Element 20 is present in the unordered_multiset Count of element 20: 3
Checking Elements in an Empty unordered_multiset
In this example, we are checking the result if we search an element in an empty unordered multiset −
#include <iostream>
#include <unordered_set>
using namespace std;
int main() {
// empty container
unordered_multiset<int> ums;
cout << "Size of unordered_multiset: " << ums.size() << endl;
cout << "Is 10 present? : ";
if (ums.contains(10))
cout << "Yes\n";
else
cout << "No\n";
cout << "Is 5 present? : ";
if (ums.contains(5))
cout << "Yes\n";
else
cout << "No\n";
return 0;
}
The output of the above code is given below −
Size of unordered_multiset: 0 Is 10 present? : No Is 5 present? : No
Using contains() with String Unordered_multiset
Here is an example to check the existence of string elements in an unordered_multiset words using the unordered_multiset::contains() function −
#include <iostream>
#include <unordered_set>
#include <string>
using namespace std;
int main() {
unordered_multiset<string> words = {"apple", "banana", "cherry", "banana"};
cout << "Unordered_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 unordered_multiset" << endl;
else
cout << "'" << search1 << "' is not present in the unordered_multiset" << endl;
if (words.contains(search2))
cout << "'" << search2 << "' is present in the unordered_multiset" << endl;
else
cout << "'" << search2 << "' is not present in the unordered_multiset" << endl;
return 0;
}
The output of the above code is given below −
Unordered_multiset elements: banana banana cherry apple 'banana' is present in the unordered_multiset 'mango' is not present in the unordered_multiset
Run the above example codes in the compilers that supports C++20 and above.
Difference Between contains() and find() Function
The key differences between the unordered_multiset::contains() and unordered_multiset::find() functions in C++ STL unordered_multiset is given below −
| unordered_multiset::contains() | unordered_multiset::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 unordered_multiset or not. | You can use the iterator to access or erase the element. |
| Since it returns a boolean value, it is useful in conditional statements like if-else. | Since it returns an iterator, it is useful when you want to perform any operation on specific value. |
| It was introduced in C++20. | It was introduced in C++11. |