Skip to Content
C++の紹介 (Japanese Edition)
book

C++の紹介 (Japanese Edition)

by Frances Buontempo
March 2026
Intermediate
348 pages
4h 30m
Japanese
O'Reilly Media, Inc.
Content preview from C++の紹介 (Japanese Edition)

第15章 テンプレート とstd::unordered_map

この作品はAIを使って翻訳されている。ご意見、ご感想をお待ちしている:translation-feedback@oreilly.com

この最終章では、ゲーム内で各イベントタイプがどれだけ発生したかをトラッキングするためにルックアップテーブルを使う。 ゲーム中に各イベントの発生頻度を集計し、終了後に報告する。 この集計はゲーム自体に何も追加しないが、別の有用なC++コンテナを学ぶことになる。 イベントごとの頻度を保持するためにstd::unordered_mapを使用する。つまり、検索を容易にするために独自のテンプレートを書く必要がある。 検索テーブルをレパートリーに加え、テンプレートの書き方を理解すれば、C++の幅広い分野で確かな基礎が身につく。 集計結果を使って、リクエストした確率で異なるイベントが発生しているか確認できる。

また、型に対する等価演算子のデフォルト実装やstd::pairについて学び、別のビジターを書く機会も得られる。 ここまででC++の多くの部分をカバーした。最後に細かい点をいくつかまとめよう。 全てを網羅したわけではない——C++は巨大な言語だ——だが、プログラム全体を書くのに十分な知識は身についた。調べれば分かることも理解している。

ルックアップテーブルの作成

ルックアップテーブルは、他の言語では辞書と呼ばれることもある。 これは連想コンテナの一種であり、高速な検索を提供するコンテナだ。 ルックアップテーブルはキーと値のペアを含む。 キーはEventであり、値は集計結果となる。

C++では一意のキーを一意な値にマッピングする2種類の型を提供する。 古いコンテナは<MAP>ヘッダで定義されるstd::mapだ。 このコンテナは要素の順序付けに比較を使用するため、例えば順序付けられていないstd::vectorを反復処理するよりも検索が速い。 最新のは<UNORDERED_MAP>ヘッダで定義されるstd::unordered_mapだ。 この非順序版は検索スピード向上のため異なる手法を用いる。 詳細を見ていこう。

std::unordered_mapにはキーと値を指定する必要がある。順序は次の通りだ:

std::unordered_map<std::string, int> lookup;

このルックアップは std::string をintにマッピングする。 値の取得と設定にはoperator[]を使える:

lookup["Hello"] = 1; 1
int count = lookup["Hello"]; 2
1

キー「Hello」に値1をセットする

キー「Hello」の値を取得する

値が存在しない場合、デフォルトのキーが返される。 この場合、[]演算子は実際には値を挿入する。 ...

Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.

Read now

Unlock full access

More than 5,000 organizations count on O’Reilly

AirBnbBlueOriginElectronic ArtsHomeDepotNasdaqRakutenTata Consultancy Services

QuotationMarkO’Reilly covers everything we've got, with content to help us build a world-class technology community, upgrade the capabilities and competencies of our teams, and improve overall team performance as well as their engagement.
Julian F.
Head of Cybersecurity
QuotationMarkI wanted to learn C and C++, but it didn't click for me until I picked up an O'Reilly book. When I went on the O’Reilly platform, I was astonished to find all the books there, plus live events and sandboxes so you could play around with the technology.
Addison B.
Field Engineer
QuotationMarkI’ve been on the O’Reilly platform for more than eight years. I use a couple of learning platforms, but I'm on O'Reilly more than anybody else. When you're there, you start learning. I'm never disappointed.
Amir M.
Data Platform Tech Lead
QuotationMarkI'm always learning. So when I got on to O'Reilly, I was like a kid in a candy store. There are playlists. There are answers. There's on-demand training. It's worth its weight in gold, in terms of what it allows me to do.
Mark W.
Embedded Software Engineer

You might also like

エージェントメッシュ (Japanese Edition)

エージェントメッシュ (Japanese Edition)

Eric Broda, Davis Broda

Publisher Resources

ISBN: 0642572352653