GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • cpprefjp - C++日本語リファレンス
  • GitHub Project

This view

  • Original page
  • New address

GeezerVison is showing cpprefjp.github.io — std::unordered_map::bucket - cpprefjp C++日本語リファレンス

  1. リファレンス
  2. unordered_map
  3. unordered_map
  4. bucket

最終更新日時: 2026年09月16日 21時57分54秒 (JST)
rotarymars が更新

履歴 編集

function
<unordered_map>

std::unordered_map::bucket(C++11)

size_type bucket(const key_type& k) const;           // (1) C++11
constexpr size_type bucket(const key_type& k) const; // (1) C++26

template <class K>
constexpr size_type bucket(const K& k) const; // (2) C++26

概要

指定したキーと等価な要素が格納されている場合、そのバケットのインデックス(添え字)を取得する。

  • (1) : key_type型のキーを受け取って、バケットのインデックスを取得する
  • (2) : key_typeと比較可能なキーを受け取って、バケットのインデックスを取得する

テンプレートパラメータ制約

  • (2) : Hash::is_transparentおよびPred::is_transparentが妥当な式であること

事前条件

当該コンテナは bucket_count() > 0 であること

戻り値

パラメータ k と等価なキーの要素が格納されているバケットのインデックス(添え字)

事後条件

戻り値は [0, bucket_count()) の範囲である。

計算量

定数。

備考

  • 指定したキーと等価な要素が格納されていない場合、そのキーを挿入した際に rehash が発生しなければ格納されるバケットのインデックス(添え字)が返る。
  • (2) :
    • is_transparentは、HashとPredの両方がメンバ型として持つ必要がある。Predにはis_transparentを定義するstd::equal_to<>が使えるが、Hashには標準ライブラリに該当するものがないため、利用者が定義する必要がある。詳細はstd::hashクラスのページを参照。
    • これらのオーバーロードは、unordered_map<string, int>のようなコンテナに対し、検索操作で文字列リテラルを渡した際に、キー型の一時オブジェクトが生成されるコストを減らすためにある。

例

#include <iostream>
#include <unordered_map>

int main()
{
  std::unordered_map<char, int> um = {
    {'A', 1},
    {'B', 2},
    {'C', 3},
    {'D', 4},
    {'E', 5}
  };

  decltype(um)::size_type c = um.bucket_count();
  std::cout << "bucket_count() = " << c << std::endl;

  // 全てのキーに対するバケットのインデックスとそのバケットの要素数を取得
  for (decltype(um)::const_reference x : um) {
    decltype(um)::key_type k = x.first;
    decltype(um)::size_type b = um.bucket(k);
    decltype(um)::size_type s = um.bucket_size(b);
    std::cout << "key = " << k << ", bucket = " << b << ", bucket_size = " << s << std::endl;
  }

  // 存在しないキーに対するバケットのインデックスとそのバケットの要素数を取得
  decltype(um)::key_type k = 'H';
  decltype(um)::size_type b = um.bucket(k);
  decltype(um)::size_type s = um.bucket_size(b);
  std::cout << "key = " << k << ", bucket = " << b << ", bucket_size = " << s << std::endl;
}

出力例

bucket_count() = 11
key = E, bucket = 3, bucket_size = 1
key = A, bucket = 10, bucket_size = 1
key = B, bucket = 0, bucket_size = 1
key = C, bucket = 1, bucket_size = 1
key = D, bucket = 2, bucket_size = 1
key = H, bucket = 6, bucket_size = 0

バージョン

言語

  • C++11

処理系

  • Clang: 3.1 ✅
  • GCC: 4.7.2 ✅
  • Visual C++: ?

関連項目

名前 説明
max_bucket_count 最大バケット数の取得

参照

  • P2363R5 Extending associative containers with the remaining heterogeneous overloads
    • C++26でtemplate <class K>のバージョンが追加された
  • P3372R3 constexpr containers and adaptors
  • LWG Issue 1197. Can unordered containers have bucket_count() == 0?
    • C++11で、bucket_count()> 0という事前条件が追加された。バケット数が0の場合、剰余によるバケット決定が行えないため
You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.