- The C Standard Library
- The C Standard Library
- The C++ Standard Library
- 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>
- C++ Programming Resources
- C++ Programming Tutorial
- C++ Useful Resources
- C++ Discussion
C++ Unordered_set::bucket_size() Function
The C++ unordered_set::bucket_size() function is used to return the number of elements present in the n the bucket. A bucket is a slot in the unordered_set container's internal hash table to which elements are assigned based on the hash value. Buckets have numbers ranging from 0 to (bucket_count - 1).
Syntax
Following is the syntax of std::unordered_set::bucket_size() function.
size_type bucket_size ( size_type n ) const;
Parameters
- n − It indicates the bucket number that must be less than the bucket_count.
Return Value
This function returns the number of elements in the bucket.
Example 1
Let's look at the following example, where we are going to demonstrte the usage of unordered_set::bucket_size() function.
#include <iostream> #include <string> #include <unordered_set> int main () { std::unordered_set<std::string> myUset = { "sai", "ram", "krishna", "prasad", "tutorials", "point" }; unsigned nbuckets = myUset.bucket_count(); std::cout << "myset has " << nbuckets << " buckets:\n"; for (unsigned i = 0; i < nbuckets; ++i) { std::cout << "bucket #" << i << " has " << myUset.bucket_size(i) << " elements.\n"; } return 0; }
Output
If we run the above code it will generate the following output −
myset has 13 buckets: bucket #0 has 0 elements. bucket #1 has 0 elements. bucket #2 has 0 elements. bucket #3 has 1 elements. bucket #4 has 0 elements. bucket #5 has 1 elements. bucket #6 has 0 elements. bucket #7 has 0 elements. bucket #8 has 1 elements. bucket #9 has 2 elements. bucket #10 has 0 elements. bucket #11 has 1 elements. bucket #12 has 0 elements.
Example 2
Consider the following example, where we are going to find the number of elements that specified bucket contains.
#include <iostream> #include <string> #include <unordered_set> using namespace std; int main () { unordered_set<string> myUset = { "sai", "ram", "krishna", "prasad", "tutorials", "point" }; unsigned nbuckets = myUset.bucket_count(); cout << "myUset has " << nbuckets << " buckets:\n"; cout<<"bucket 5 contains: "<<myUset.bucket_size(5); return 0; }
Output
Following is the output of the above code −
myUset has 13 buckets: bucket 5 contains: 1
Example 3
In the following example, we are going to consider the unordered_set of string type and applying the bucket_size() function to count the number of elements in each bucket and displaying them.
#include <iostream> #include <string> #include <unordered_set> using namespace std; int main () { unordered_set<string> uSet = { "red", "green", "blue", "yellow", "purple", "pink" }; unsigned nbuckets = uSet.bucket_count(); cout << "uSet has " << nbuckets << " buckets:\n"; for (unsigned i=0; i<nbuckets; ++i) { cout << "bucket #" << i << " has " << uSet.bucket_size(i) << " elements: "; for (auto it = uSet.begin(i); it!=uSet.end(i); ++it) cout << "[" << *it<< "] "; cout << "\n"; } return 0; }
Output
Output of the above code is as follows −
uSet has 13 buckets: bucket #0 has 2 elements: [purple] [red] bucket #1 has 0 elements: bucket #2 has 1 elements: [blue] bucket #3 has 1 elements: [green] bucket #4 has 0 elements: bucket #5 has 0 elements: bucket #6 has 1 elements: [yellow] bucket #7 has 0 elements: bucket #8 has 0 elements: bucket #9 has 0 elements: bucket #10 has 1 elements: [pink] bucket #11 has 0 elements: bucket #12 has 0 elements: