We have used vectors several times now but haven’t used an associative container yet. An associative container holds key-value pairs, giving us a lookup table or dictionary. In this chapter, we will use dictionaries to create a game of answer smash. We’ll provide two clues, each a definition of a word. The end of the first word will overlap with the start of the next word, giving the answer. For example, a vector is a “sequential container supporting dynamic resizing,” and a torch could be defined as a “lit stick carried in one’s hand,” so smashing together the words vector and torch gives the answer vectorch.
We’ll start by storing a dictionary in an std::map defined in the map header, which existed before C++11, and then consider other types of associative containers too. We will use the newer std::unordered_map in the next chapter, so using std::map in this chapter will be a useful revision, and we will learn about an std::pair and the more general std::tuple on the way. We will start with hardcoded dictionaries and read data from a file afterward using a random sample to create variety when we play the game.