- 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 - <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++ 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)