
- 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 - <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++ unordered_set::bucket_count() Function
The C++ unordered_set::bucket_count() function is used to return the number of buckets in the unordered_set container. A bucket is a slot in the container's internal hash table to which elements are assigned based on the hash value of their key. They have a number ranging from 0 to bucket_count - 1.
Syntax
Following is the Syntax of std::unordered_set::bucket_count() function.
size_type bucket_count() const noexcept;
Parameters
This function does not accepts any parameter.
Return Value
This function returns the total number of bucket present in the unordered_set.
Example 1
Let's look at the following example, where we are going to demonstrate the usage of unordered_set::bucket_count() function.
#include <iostream> #include <unordered_set> using namespace std; int main(void){ unordered_set<char> uSet = {'a', 'b', 'c', 'd'}; cout << "Number of buckets = " << uSet.bucket_count() << endl; return 0; }
Output
If we run the above code it will generate the following output −
Number of buckets = 13
Example 2
Consider the following example, where we are going to use the unordered_set::bucket_count() function to get the total number of buckets alongs with a no.of items.
#include <iostream> #include <string> #include <unordered_set> using namespace std; int main () { unordered_set<string> uSet = {"Aman","Garav", "Sunil", "Roja", "Revathi"}; unsigned n = uSet.bucket_count(); cout << "uSet has " << n << " buckets. \n"; for (unsigned i=0; i<n; ++i) { cout << "bucket #" << i << " contains: "; for (auto it = uSet.begin(i); it!=uSet.end(i); ++it) cout << "[" << *it << "] "; cout << "\n"; } return 0; }
Output
Following is the output of the above code −
uSet has 13 buckets. bucket #0 contains: bucket #1 contains: bucket #2 contains: bucket #3 contains: bucket #4 contains: bucket #5 contains: [Roja] [Garav] bucket #6 contains: bucket #7 contains: bucket #8 contains: [Aman] bucket #9 contains: bucket #10 contains: [Revathi] bucket #11 contains: bucket #12 contains: [Sunil]
Example 3
In the following example, we are going to count the number of buckets and buckets_size() to count the number of elements in each bucket.
#include <iostream> #include <unordered_set> using namespace std; int main() { unordered_set<char> uSet; uSet.insert({'a', 'b', 'c', 'd', 'e'}); int n = uSet.bucket_count(); cout << "uSet has " << n << " buckets.\n\n"; for (int i = 0; i < n; i++) { if(uSet.bucket_size(i)>0) cout << "Bucket " << i << " has "<< uSet.bucket_size(i) << " elements.\n"; } return 0; }
Output
Output of the above code is as follows −
uSet has 13 buckets. Bucket 6 has 1 elements. Bucket 7 has 1 elements. Bucket 8 has 1 elements. Bucket 9 has 1 elements. Bucket 10 has 1 elements.