CKTIS / C++
std::unordered_multiset — a hash container allowing duplicates
std::unordered_multiset stores keys while allowing multiple equivalent occurrences. It supports lookup and removal without maintaining sorted order, and requires the hash function to agree with the key equivalence predicate.
Version: C++23; repository compiler flag, production runtime not attested
Syntax
#include <unordered_set>
std::unordered_multiset<Key> values;
std::unordered_multiset<Key, Hash, Pred, Allocator> custom;std::unordered_multiset<Key>, declared in <unordered_set>, is an unordered associative container: each element is also its key. Unlike std::unordered_set, it allows duplicates. The parameters Hash, Pred, and Allocator default to std::hash<Key>, std::equal_to<Key>, and std::allocator<Key>.
insert adds another occurrence. find(k) identifies one match or returns end(), count(k) counts matches, and equal_range(k) identifies their range. erase(k) removes all matches and returns their number; erase(it) removes only the indicated element. Never pass end() to erase(it).
Iteration order is unspecified, but equivalent keys are adjacent. Pred must define an equivalence relation, and equivalent keys must have the same hash. Elements cannot be modified through iterators. Rehashing invalidates iterators, but not pointers or references to elements.
This entry targets C++23. The supplied document is an evolving draft, not a frozen C++23 specification; newer constexpr declarations must not be treated as C++23 guarantees. The repository compiler flag does not attest operation in a production runtime.
Examples
#include <iostream>
#include <unordered_set>
int main() {
std::unordered_multiset<int> values{4, 2, 4};
values.insert(4);
std::cout << "count: " << values.count(4) << '\n';
auto it = values.find(4);
if (it != values.end()) {
values.erase(it);
}
std::cout << "remaining: " << values.count(4) << '\n';
const auto removed = values.erase(4);
std::cout << "removed: " << removed << '\n';
std::cout << "size: " << values.size() << '\n';
}
Result
count: 3 remaining: 2 removed: 2 size: 1
The displayed code and declared result were verified in an isolated environment using the method recorded in the verification evidence for this exact version.