- 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 Library - size() Function
Description
The unordered_multiset::size() function in C++ STL returns the number of elements present in the unordered_multiset container. It does not accept any parameters. It also returns the count of duplicate elements present in the unordered_multiset.
Syntax
Following is the syntax of unordered_multiset::size() function −
size_type size() const noexcept; //C++11 onwards size_type size() const constexpr; // C++26 onwards
Return value
It returns a value of type size_type.
Exceptions
It never throws exceptions.
Time complexity
The time complexity of unordered_multiset::size() function is O(1) (constant).
Examples of unordered_multiset::size() Function
The following examples demonstrate the usage of unordered_multiset::size() function in unordered_multiset −
Getting the Size of unordered_multiset
In this example, we are getting the number of elements in an unordered_multiset in three different scenarios using the unordered_multiset::size() function. We have got the initial size of unordered_multiset, after inserting two more elements, and after removing elements from unordered_multiset.
#include <iostream>
#include <unordered_set>
using namespace std;
void printUnorderedMultiset(const unordered_multiset<int>& numbers) {
for (auto it = numbers.begin(); it != numbers.end(); ++it)
cout << " " << *it;
cout << endl;
}
int main(){
unordered_multiset<int> numbers = {10, 20, 30, 20, 5, 10, 20};
cout << "Initial unordered_multiset elements:";
printUnorderedMultiset(numbers);
cout << "Initial size: " << numbers.size() << endl;
numbers.insert(40);
numbers.insert(20);
cout << "Unordered_multiset elements after insertions:";
printUnorderedMultiset(numbers);
cout << "Size after insertions: " << numbers.size()
<< endl;
numbers.erase(20);
cout << "Unordered_multiset elements after removing 20:";
printUnorderedMultiset(numbers);
cout << "Size after removing 20 from unordered_multiset: "
<< numbers.size() << endl;
return 0;
}
The output of the above code is given below −
Initial unordered_multiset elements: 20 20 20 30 10 10 5 Initial size: 7 Unordered_multiset elements after insertions: 20 20 20 20 40 30 10 10 5 Size after insertions: 9 Unordered_multiset elements after removing 20: 40 30 10 10 5 Size after removing 20 from unordered_multiset: 5
Using size() to Check if unordered_multiset is Full
In this example, a maximum capacity of container is defined. We get the size of unordered_multiset using the size() function and compare it with the maximum capacity. If the value of maximum capacity is same, then the container is full.
#include <iostream>
#include <unordered_set>
using namespace std;
int main() {
unordered_multiset<string> characters;
int capacity = 3;
characters.insert("Luffy");
characters.insert("Zoro");
characters.insert("Shanks");
cout << "Maximum characters allowed: " << characters.size();
cout << "\nCharacters list:";
for (const auto &characters : characters)
cout << " " << characters;
if (characters.size() >= capacity)
cout << "\nList is full." << endl;
else
cout << "\nYou can add more characters." << endl;
return 0;
}
The output of the above code is given below −
Maximum characters allowed: 3 Characters list: Shanks Zoro Luffy List is full.
Using size() to Check if unordered_multiset is Empty
The following example checks if the unordered_multiset is empty or not. We get the size of container using the size() function and check if the size value is 0. If the value is 0, then the container is empty.
#include <iostream>
#include <unordered_set>
using namespace std;
int main() {
unordered_multiset<int> numbers;
cout << "Unordered_multiset elements:";
for (auto it = numbers.begin(); it != numbers.end(); ++it)
cout << " " << *it;
if (numbers.size() == 0)
cout << "\nUnordered_multiset is empty.\n";
return 0;
}
The output of the above code is given below −
Unordered_multiset elements: Unordered_multiset is empty.