Become Comfortable With Maps So You Will Want To Use Them Instead Of Avoiding Them

A map is a completely different type of collection than a vector or list. It solves a different purpose. If you just want to put some things in a container and pull them all back out later, then a vector or a list is a good choice. Or if you want the contents to stay in a particular order that you choose, then again, a vector or a list is a good choice.

But if you have hundreds, thousands, or even millions of things in a collection and you need to find one of them quickly, that’s when you can use a map.

Have you ever played 20 questions? It’s a game where you try to guess what another person is thinking of. The strategy to win is to choose good questions. Questions that help you eliminate as many wrong answers as possible.

If you’re trying to figure out how big something is, don’t start by asking if it’s smaller than a centimeter, then if it’s smaller than two centimeters, then three, etc.

This would be like looking for what you want in a vector by starting with the first item, then looking at the second item, then the third, etc. until you find the one you want.

What you want is to pick a value in the middle of what you might expect. Let’s say that you think the size could be anywhere from a centimeter up to ten meters. A good place to start would be to ask if it’s smaller than five meters. This lets you eliminate half the options with just one question. Then you just keep asking questions that divide and eliminate. You’ll find that you can usually get close or arrive at what the other person is thinking of with just a few questions.

With a map collection, you put things in the collection together with a key and you use the value of the key to help you find what you want later. It’s not about the questions anymore. The map will figure out how to find the items by placing each item in a carefully chosen location. And it’s really good at it. This is why a map can’t be relied upon to keep things in some order or another. It chooses the order so it can find things.

If you have a million things in your map, then it should be able to find just the one you want by looking at no more than 13. That’s a lot better than looking through items in a vector one by one.

Now, because you have to work with both the things you put into a map and the keys needed to identify them, a map is a little more involved to use than a vector.

You’ll learn how to use a map just like how you learned to use a vector and a list in the previous class. But you’ll also learn the differences that you’ll need to adapt to in order to use a map.

In particular, you’ll learn how to simplify your code with “using” statements to define the types that the map needs. This is a pair of the key and the value. And it can make your code a lot harder to read if you don’t define your own types.

You’ll learn how to use the types you define to declare maps and to add things to them. And then to find things again.

And you’ll learn how to remove items and tell if a map is empty.

And finally, you’ll learn when to use a map and when to use another similar collection called an unordered_map. I know. I already said that a map doesn’t have an order to it. Let me clarify that. A map does have an order. It’s just not any order based on where you put things in the map. That’s because you don’t put things in a map at a specific location like you do with a vector or a list. The map manages the location for you. It has an order that it defines. The order should resemble a pyramid.

An unordered_map behaves quite similar to a map. It’s how it’s built inside that’s different. And this difference gives you an ability to find things even quicker sometimes. You can think of this order resembling a line of mailboxes or numbered parking spaces.

Find Available Dates

Once you purchase one of the classes, you can reserve a spot in any of the upcoming dates for that class. Use the button below to easily search for the next available dates.


There are no reviews yet.

Only logged in customers who have purchased this product may leave a review.

You may also like…