Maps in C++ (std::map and std::unordered_map)

preview_player
Показать описание


Chapters:
----------------
0:00 - What are maps?
3:40 - Why use maps + example usage
9:30 - Writing a hash function to use a custom type
16:50 - The [] operator
19:00 - The .at() function
20:40 - How to check if key exists in map
21:11 - How to iterate through maps
24:47 - How to remove entries from maps
25:03 - Writing a less-than operator for custom types
28:05 - Performance and which map to use

This video is sponsored by Hostinger.
Рекомендации по теме
Комментарии
Автор

Hope you all enjoyed the latest addition to the C++ series! Let me know if you have any questions about maps below 👇

TheCherno
Автор

21:01 - When using find(), you can avoid the second lookup required by at() by saving the iterator returned by find(). For example:

if (const auto it = cities.find("Berlin"); it != cities.end()) {
const CityRecord& berlinData = it->second;
// ...
}

I recommend always doing this whenever you use find() to do a lookup. If you only need to know whether the element exists or not, you can use ".count(key) != 0" (or ".contains(key)" in C++20) instead.

SonicFan
Автор

15:20 A hash function is not required to return a unique hash. Two things with the same hash will be stored in the same bucket, which is inefficient for lookups, but it will function fine. The hash provides fast access to the appropriate bucket. The equality operator is what determines if a key actually matches. You should strive to make unique hashes for performance reasons, but hash collisions will not compromise the map.

26:50 -If two cities have the same population and that is all you are using for comparison, then they will appear in an arbitrary order but still appear next to each other in the map. Nothing is broken in this case. You just will not be able to rely on their order remaining consistent as the map changes.- This is incorrect. Not sure what I was thinking when I wrote it.

crystalferrai
Автор

Yes! This C++ series is truly a gold mine :D

arminbosten
Автор

Hey Cherno, I started watching you 5 years ago, while I was in my first year of college. I saw every video you made. I patiently rewrote line by line your sparky engine, but then I had so many school duties and I had to start working to pay for the college, so that I had to stop following you. Now 5 years later I'm back and ready to continue. I don't know what is so magical about working with OpenGL and graphics, but it's still my dream job to work with OpenGL. I tried many specialization like neural nets, hardware design in VHDL, motor control, image processing but nothing got me interested as OpenGL. Best part of college was C++ course, where I applied everything you taught me. Thank you so much for your content and god bless your family.

AxElKo
Автор

100/100 c++ videos from this playlist watched!

Been very informative so thanks for this series!

milo
Автор

The find + at pattern at 20:50 does twice the amount of necessary work, because find already returns a pointer to the data.

oj
Автор

Hey Cherno, would love a “productivity in c++” kind of video maybe going over keyboard shortcuts or ways to code more efficiently in visual studio or just in general

CamdenVaughan
Автор

Expecting more content in this C++ series. This series is a gem. 🔥

gaureshbhati
Автор

Oh nice. Finally a video for the C++ series. I got so excited when I saw the notification.

ohwow
Автор

great video, thanks.
the great thing about your videos is that you go deep, and investigate different aspects of the subject that you are talking about.
waiting for more videos and tutorials...

erfanamkh
Автор

One important thing to mention is that the std:: (unordered-)map/set containers are very slow, because they need to guarantee pointer stability and abi breaks hinder compiler vendors from improving them. You can get speedups of sometimes up to 5x by just using a different hash map, e.g. absl::flat_hash_map or tsl::robin_map.

oj
Автор

Finally a C++ series tutorial with an old style thumbnail!

ykjo
Автор

I love the honesty of how you handled the odditites in the industry, I love that you explain multiple ways of doing something even if there is a less conventional way as a just in case, I love that you actually explain how to read the error messages and output errors and most of all I love your accent. Thank you so much for being clear and connecting dots.

SHSULeadTrumpet
Автор

Never would've thought I'd learn basic c++ in two weeks knowing just js but here we are. This series is wild.

leeroyjenkns
Автор

Thanks for your videos Cherno. I only just stumbled across your channel and your videos have been a really useful recap for my rusty C++ skills from over a decade ago!

londonerwalks
Автор

Great video! A follow up video about std::set in which you compare the different sets to the different maps would be awesome! Keep it up!

wintiepower
Автор

100th video of the c++ series. Well done Yan!!

MLBB_CHILL
Автор

15:51 What you're trying to do is called aggregate initialization. It requires you to also use curly brackets. Unfortunately you'll need to use push_back instead of emplace_back because the parameters aren't passed through, but the object is first created and then passed through.

Thanks for the great content. Keep them coming!!

bunpasi
Автор

That was actually perfect for a beginner. Thank you!

adrianwild