
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Hello World
- C++ Omitting Namespace
- C++ Tokens
- C++ Constants/Literals
- C++ Keywords
- C++ Identifiers
- C++ Data Types
- C++ Numeric Data Types
- C++ Character Data Type
- C++ Boolean Data Type
- C++ Variable Types
- C++ Variable Scope
- C++ Multiple Variables
- C++ Basic Input/Output
- C++ Modifier Types
- C++ Storage Classes
- C++ Numbers
- C++ Enumeration
- C++ Enum Class
- C++ References
- C++ Date & Time
- C++ Operators
- C++ Arithmetic Operators
- C++ Relational Operators
- C++ Logical Operators
- C++ Bitwise Operators
- C++ Assignment Operators
- C++ sizeof Operator
- C++ Conditional Operator
- C++ Comma Operator
- C++ Member Operators
- C++ Casting Operators
- C++ Pointer Operators
- C++ Operators Precedence
- C++ Unary Operators
- C++ Control Statements
- C++ Decision Making
- C++ if Statement
- C++ if else Statement
- C++ Nested if Statements
- C++ switch Statement
- C++ Nested switch Statements
- C++ Loop Types
- C++ while Loop
- C++ for Loop
- C++ do while Loop
- C++ Foreach Loop
- C++ Nested Loops
- C++ break Statement
- C++ continue Statement
- C++ goto Statement
- C++ Strings
- C++ Strings
- C++ Loop Through a String
- C++ String Length
- C++ String Concatenation
- C++ String Comparison
- C++ Functions
- C++ Functions
- C++ Multiple Function Parameters
- C++ Recursive Function
- C++ Return Values
- C++ Function Overloading
- C++ Function Overriding
- C++ Default Arguments
- C++ Arrays
- C++ Arrays
- C++ Multidimensional Arrays
- C++ Pointer to an Array
- C++ Passing Arrays to Functions
- C++ Return Array from Functions
- C++ Structure & Union
- C++ Structures
- C++ Unions
- C++ Pointers
- C++ Pointers
- C++ Dereferencing
- C++ Modify Pointers
- C++ Class and Objects
- C++ Object Oriented
- C++ Classes & Objects
- C++ Class Member Functions
- C++ Class Access Modifiers
- C++ Static Class Members
- C++ Static Data Members
- C++ Static Member Function
- C++ Inline Functions
- C++ this Pointer
- C++ Friend Functions
- C++ Pointer to Classes
- C++ Constructors
- C++ Constructor & Destructor
- C++ Default Constructors
- C++ Parameterized Constructors
- C++ Copy Constructor
- C++ Constructor Overloading
- C++ Constructor with Default Arguments
- C++ Delegating Constructors
- C++ Constructor Initialization List
- C++ Dynamic Initialization Using Constructors
- C++ Inheritance
- C++ Inheritance
- C++ Multiple Inheritance
- C++ Multilevel Inheritance
- C++ Object-oriented
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces
- C++ Virtual Function
- C++ Pure Virtual Functions & Abstract Classes
- C++ File Handling
- C++ Files and Streams
- C++ Reading From File
- C++ Advanced
- C++ Exception Handling
- C++ Dynamic Memory
- C++ Namespaces
- C++ Templates
- C++ Preprocessor
- C++ Signal Handling
- C++ Multithreading
- C++ Web Programming
- C++ Socket Programming
- C++ Concurrency
- C++ Advanced Concepts
- C++ Lambda Expression
- C++ unordered_multiset
C++ unordered_multiset
std::unordered_multiset
An unordered_multiset is a container by the Standard Template Library (STL) in C++, which stores elements without any particular order and allows multiple occurrences or duplicate values of the same element. The <unordered_set> header file is used for both unordered_set and unordered_multiset containers.
Syntax
Here is the following syntax for declaring an unordered_multiset:
#include <unordered_set> std::unordered_multiset<type> container_name;
Here,
-
<type> is the data type of the elements you want to store in the unordered_multiset (e.g., int, string, etc.).
-
container_name is the name of the unordered_multiset variable.
Example to Create unordered_multiset
The following example demonstrates how you can create an unordered multiset:
#include <unordered_set> #include <iostream> #include <string> using namespace std; int main() { // Declare unordered_multisets for integers and strings unordered_multiset<int> ums_int = {1, 2, 3, 3}; unordered_multiset<string> ums_str = {"apple", "banana", "apple"}; // Display both sets together (merged display) cout << "Merged unordered_multiset (Integers and Strings): "; for (const auto& elem : ums_int) { cout << elem << " "; } for (const auto& elem : ums_str) { cout << elem << " "; } cout << endl; return 0; }
Output
Merged unordered_multiset (Integers and Strings): 3 3 2 1 banana apple apple
Member Functions of unordered_multiset
1. The insert() function
The insert() function is used to add one or more elements to the unordered_multiset, allowing duplicity.
Syntax
unordered_multiset<type> ums; ums.insert(element); ums.insert({element1, element2, ...});
2. The find() function
The find() function is used to check if an element exists in the unordered_multiset. It returns an iterator to the element if found; otherwise, it returns end()
.
Syntax
auto it = ums.find(element); // Returns iterator to element or ums.end()
3. The count() function
The count() function returns the number of occurrences of an element in the unordered_multiset. Since this function allows duplicacy, it will return the total count of that element in the container.
Syntax
size_t count = ums.count(element); // Returns number of occurrences of element
4. The erase() function
The erase() function removes one or more elements from the unordered_multiset. You can remove specific elements by value or use an iterator to remove a single element. For duplicates, only one occurrence will be removed for each call.
Syntax
ums.erase(element); // Removes one occurrence of element ums.erase(it); // Removes the element at iterator `it` ums.erase(ums.begin(), ums.end()); // Removes all elements in the range
5.The find() or count() function
This function will check if an element exists in the container.
Syntax
list.count(element); // for lists string.count(substring); // for strings
6. The size() function
The size() function returns the number of elements currently in the unordered_multiset.
Syntax
size_t size = ums.size(); // Returns the number of elements in the set
7. The begin() and end() functions
To iterate, you can use iterators or range-based loops over the elements in an unordered_multiset. begin() and end() are used for accessing the first and last elements.
Syntax
for (auto it = ums.begin(); it != ums.end(); ++it) { // Access each element via *it }
unordered_set Vs. unordered_multiset
- unordered_set: A container that stores unique elements only, no duplicates are allowed.
- unordered_multiset: A container that allows multiple occurrences (duplicates) of the same element.
Average Time Complexity of unordered_multiset
unordered_multiset is implemented using a hash table data structure. This results in fast access because elements are hashed into "buckets" based on their hash values. This unordered_multiset provides constant time complexity, i.e., O(1), for some generic operations like lookups, insertions, and deletions.
Hash collisions lead to the worst time complexity O(n). This is because, in a collision, multiple elements are hashed into the same bucket, which results in a linear search through all the elements in that bucket. This overall results in a significant degradation of performance.
Use Cases of unordered_multiset
- It is used when you want to store multiple occurrences of the same element.
- It is suitable for counting the frequency of items or when duplicates are required.
- It is efficient for quick lookups and insertions with no need for sorting.