當前位置: 首頁>>編程示例 >>用法及示例精選 >>正文


C++ Unordered_multimap bucket()用法及代碼示例


描述

C++ 函數std::unordered_multimap::bucket()返回帶有鍵的元素所在的桶號k位於。

Bucket 是容器哈希表中的一個內存空間,元素根據其鍵的哈希值分配到該內存空間。桶的有效範圍是從0bucket_count - 1

聲明

以下是 std::unordered_multimap::bucket() 函數形式 std::unordered_map() 頭文件的聲明。

C++11

size_type bucket(const key_type& k) const;

參數

k─ 要定位其存儲桶的 key 。

返回值

返回key對應的bucket的序號k

時間複雜度

常數,即 O(1)

示例

下麵的例子展示了 std::unordered_multimap::bucket() 函數的用法。

#include <iostream>
#include <unordered_map>

using namespace std;

int main(void) {
   unordered_multimap<char, int> umm = {
            {'a', 1},
            {'b', 2},
            {'c', 3},
            {'d', 4},
            {'e', 5}
            };
   cout << "Unordered multimap contains following elements" << endl;

   for (auto it = umm.begin(); it != umm.end(); ++it) {
      cout << "Element " << "[" << it->first  << ":"
          << it->second << "] " << "is in " 
          << umm.bucket(it->first) << " bucket." << endl; 
   }

   return 0;
}

讓我們編譯並運行上麵的程序,這將產生以下結果——

Unordered multimap contains following elements
Element [e:5] is in 2 bucket.
Element [a:1] is in 9 bucket.
Element [b:2] is in 10 bucket.
Element [c:3] is in 0 bucket.
Element [d:4] is in 1 bucket.

相關用法


注:本文由純淨天空篩選整理自 C++ Unordered_multimap Library - bucket() Function。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。