问题
for std::unordered_map
, can you access the elements in some bucket i
if its bucket_size
is not zero?
回答1:
The answer is to use std::unordered_map::begin(bucket_num)
or std::unordered_map::cbegin(bucket_num)
to get the iterator pointing to the the first element to that bucket and iterate to the end of the bucket
std::unordered_map::end(bucket_num)
for ( auto it = u_map.begin(bucket_num); it!= u_map.end(bucket_num); ++it)
{
cout<<it->first<<", "<<it->second<<endl;
}
来源:https://stackoverflow.com/questions/32043034/stdunordered-map-can-you-access-the-elements-in-a-bucket-with-its-bucket-numb