public member function
<unordered_map>

std::unordered_multimap::bucket

size_type bucket ( const key_type& k ) const;
定位元素的桶
返回鍵為 k 的元素所在的桶的編號。

桶是容器內部雜湊表的一個槽,元素根據其鍵的雜湊值分配到其中。具有相同鍵的元素位於同一桶中。桶的編號從0(bucket_count-1).

桶中的單個元素可以透過 unordered_multimap::beginunordered_multimap::end 返回的範圍迭代器來訪問。

引數

k
要定位其桶的鍵。
成員型別key_type是容器中元素的鍵的型別,在 unordered_multimap 中定義為其第一個模板引數().

返回值

k 對應的桶的順序號。

成員型別size_type是一種無符號整型型別。

示例

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
// unordered_multimap::bucket
#include <iostream>
#include <string>
#include <unordered_map>

int main ()
{
  std::unordered_multimap<std::string,std::string> myumm = {
    {"John","Middle East"},
    {"John","Africa"},
    {"Adam","Europe"},
    {"Bill","Norh-America"}
  };

  for (auto& x: myumm) {
    std::cout << "Element [" << x.first << ":" << x.second << "]";
    std::cout << " is in bucket #" << myumm.bucket (x.first) << std::endl;
  }

  return 0;
}

可能的輸出
Element [Adam:Europe] is in bucket #1
Element [John:Middle East] is in bucket #1
Element [John:Africa] is in bucket #1
Element [Bill:North-America] is in bucket #2


複雜度

常量。

迭代器有效性

沒有變化。

另見