// CPP program to demonstrate the
// unordered_map::end() function
// returning the elements along
// with their bucket number
#include
#include
#include <unordered_map>
using namespace std;
int main()
{
unordered_map<string, int> marks;
// Declaring the elements of the multimap
marks = { { "Rohit", 64 }, { "Aman", 37 }, { "Ayush", 96 } };
// Printing all the elements of the multimap
for (auto iter = marks.begin(); iter != marks.end(); ++iter) {
cout << "Marks of " << iter->first << " is "
<< iter->second << " and his bucket number is "
<< marks.bucket(iter->first) << endl;
}
return 0;
}
How this for loop is printing bucket no.3 two times since at every bucket it takes a pair and then it reaches to next bucket so how this for loop is able to take two pairs
// CPP program to demonstrate the
// unordered_map::end() function
// returning all the elements of the multimap
#include
#include
#include <unordered_map>
using namespace std;
int main()
{
unordered_map<string, int> marks;
// Declaring the elements of the multimap
marks = { { "Rohit", 64 }, { "Aman", 37 }, { "Ayush", 96 } };
// Printing all the elements of the multimap
cout << "marks bucket contains : " << endl;
for (int i = 0; i < marks.bucket_count(); ++i) {
cout << "bucket #" << i << " contains:";
for (auto iter = marks.begin(i); iter != marks.end(i); ++iter) {
cout << "(" << iter->first << ", " << iter->second << "), ";
}
cout << endl;
}
return 0;
}
i am able to understand this code with two for loops but i am not able to understand how a single for loop is able to print both pairs of single bucket