Skip to Content
Introducing C++
book

Introducing C++

by Frances Buontempo
March 2026
Beginner
348 pages
7h 27m
English
O'Reilly Media, Inc.
Content preview from Introducing C++

Chapter 15. Templates and std::unordered_map

In this final chapter, you will use a lookup table to track how many of each Event type happens in your game. You will tally the frequency of each Event during the game and report back afterward. The tally won’t add anything to the game itself, but it will show you another useful C++ container. You will use a std::unordered_map to keep the frequencies per Event, and that means you need to write your own template to facilitate the lookup. Adding lookup tables to your repertoire and knowing how to write templates will leave you with a firm grounding in a range of C++. You can use the tally to check that you get the different Events with the probabilities you requested.

You will also learn about defaulting the equality operator for your types and about the std::pair, and you’ll get the chance to write another visitor. You’ve covered a lot of C++ now, so let’s finish up with a few last details. I haven’t covered everything—​C++ is a big language—​but you know enough to write a whole program, and you know where to look things up.

Making a Lookup Table

Lookup tables are sometimes called dictionaries in other languages. They are a type of associative container: a container that provides fast lookup. A lookup table contains key-value pairs. Your key will be an Event, and the value will be the tally.

C++ provides two types that map unique keys to unique values. The older container is the std::map, defined in the <map> header. This container ...

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

Effective Modern C++

Effective Modern C++

Scott Meyers
C++ Software Design

C++ Software Design

Klaus Iglberger
Learn C++ by Example

Learn C++ by Example

Frances Buontempo

Publisher Resources

ISBN: 9781098178130Errata Page