C++ 無序集合庫 - bucket



描述

它返回元素值為 k 的所在桶的編號。

宣告

以下是 std::unordered_set::bucket 的宣告。

C++11

size_type bucket ( const key_type& k ) const;

引數

k − 包含桶值資訊。

返回值

它返回元素值為 k 的所在桶的編號。

異常

如果任何元素比較物件丟擲異常,則丟擲異常。

請注意,無效引數會導致未定義行為。

時間複雜度

常數時間。

示例

以下示例演示了 std::unordered_set::bucket 的用法。

#include <iostream>
#include <string>
#include <unordered_set>

int main () {
   std::unordered_set<std::string> myset = {"sai","ram","krishna","prasad"};

   for (const std::string& x: myset) {
      std::cout << x << " is in bucket #" << myset.bucket(x) << std::endl;
   }

   return 0;
}

讓我們編譯並執行上面的程式,這將產生以下結果:

prasad is in bucket #0
krishna is in bucket #2
ram is in bucket #1
sai is in bucket #3
unordered_set.htm
廣告
© . All rights reserved.