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

description
For all your web hosting needs (use coupon code CHERNO for a discount) ► https://hostinger.com/cherno Patreon ► https://patreon.com/thecherno Instagram ► https://instagram.com/thecherno Twitter ► https://twitter.com/thecherno Discord ► https://discord.gg/thecherno My C++ series ► https://www.youtube.com/playlist?list=PLlrATfBNZ98dudnM48yfGUldqGD0S4FFb 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.

detail
{'title': 'Maps in C++ (std::map and std::unordered_map)', 'heatmap': [{'end': 486.312, 'start': 468.16, 'weight': 0.797}, {'end': 596.615, 'start': 554.535, 'weight': 1}, {'end': 1280.407, 'start': 1259.039, 'weight': 0.784}, {'end': 1624.569, 'start': 1598.343, 'weight': 0.787}], 'summary': 'Covers using c++ maps for efficient data retrieval, comparing map and unordered map, discussing advantages, emphasizing hashable keys, custom type hashing, custom hashing, c++ map operations, and iterating through maps in c++, with a focus on speed, efficiency, and performance differences.', 'chapters': [{'end': 550.77, 'segs': [{'end': 52.418, 'src': 'embed', 'start': 20.637, 'weight': 1, 'content': [{'end': 24.018, 'text': 'So for example, you have some sort of ID such as like an ID number.', 'start': 20.637, 'duration': 3.381}, {'end': 27.141, 'text': "and you'd like to use that to look up the rest of the data.", 'start': 24.398, 'duration': 2.743}, {'end': 32.866, 'text': 'At its core, a map is kind of similar to an array, like a vector or a static array even.', 'start': 27.301, 'duration': 5.565}, {'end': 36.609, 'text': "If you think back to a vector, I've made a video on that, by the way, I'll have it linked up there.", 'start': 33.006, 'duration': 3.603}, {'end': 39.272, 'text': 'A vector uses an integer indexing system.', 'start': 36.629, 'duration': 2.643}, {'end': 45.918, 'text': 'Every element that we push into that vector gets an ID, which is its index inside that data structure.', 'start': 39.452, 'duration': 6.466}, {'end': 52.418, 'text': 'That works fine if you just need like ascending indices, 0, 1, 2, 3, 4, 5.', 'start': 46.118, 'duration': 6.3}], 'summary': 'A map is similar to an array, using an integer indexing system, such as 0, 1, 2, 3, 4, 5.', 'duration': 31.781, 'max_score': 20.637, 'thumbnail': 'https://coursnap.oss-ap-southeast-1.aliyuncs.com/video-capture/KiB0vRi2wlc/pics/KiB0vRi2wlc20637.jpg'}, {'end': 159.383, 'src': 'embed', 'start': 121.697, 'weight': 3, 'content': [{'end': 126.122, 'text': 'like instead of an integer in a vector, it can be used as an index to retrieve that data.', 'start': 121.697, 'duration': 4.425}, {'end': 127.624, 'text': "I know we're only scratching the surface.", 'start': 126.262, 'duration': 1.362}, {'end': 129.205, 'text': 'This is gonna be a long video, buckle down.', 'start': 127.644, 'duration': 1.561}, {'end': 132.481, 'text': "But let's dive in and take a look at a simple example to begin with.", 'start': 129.619, 'duration': 2.862}, {'end': 136.684, 'text': "Now, when we're talking about using maps in C++, there are actually two different types of maps.", 'start': 132.681, 'duration': 4.003}, {'end': 138.866, 'text': 'And we are going to talk about what they are today.', 'start': 136.744, 'duration': 2.122}, {'end': 141.208, 'text': 'We have map and we have unordered map.', 'start': 139.286, 'duration': 1.922}, {'end': 145.431, 'text': "Why are there two different types of maps? Well, because they're different types of maps.", 'start': 141.328, 'duration': 4.103}, {'end': 149.854, 'text': 'One of them is a sorted map, meaning that all the elements are actually ordered.', 'start': 145.591, 'duration': 4.263}, {'end': 152.816, 'text': 'And the other map, unordered map, is unordered.', 'start': 150.034, 'duration': 2.782}, {'end': 159.383, 'text': 'So the ordered map, which is just called map, is a self-balancing binary search tree, typically a red-black tree.', 'start': 152.937, 'duration': 6.446}], 'summary': 'Introduction to two types of maps in c++: map and unordered map, one sorted and one unordered.', 'duration': 37.686, 'max_score': 121.697, 'thumbnail': 'https://coursnap.oss-ap-southeast-1.aliyuncs.com/video-capture/KiB0vRi2wlc/pics/KiB0vRi2wlc121697.jpg'}, {'end': 234.843, 'src': 'embed', 'start': 210.006, 'weight': 0, 'content': [{'end': 217.351, 'text': 'As a basic rule of thumb, I would probably recommend that you use unordered map wherever possible, unless you need an actual sorted data structure.', 'start': 210.006, 'duration': 7.345}, {'end': 221.974, 'text': 'Because unordered map usually ends up being faster than map, or at the very least, the same speed.', 'start': 217.431, 'duration': 4.543}, {'end': 224.936, 'text': "Okay, let's create our fictional scenario example here.", 'start': 222.134, 'duration': 2.802}, {'end': 228.639, 'text': "Let's say I have some kind of city record that I want to maintain.", 'start': 225.296, 'duration': 3.343}, {'end': 230.76, 'text': 'Now, cities typically have like a name.', 'start': 228.679, 'duration': 2.081}, {'end': 234.843, 'text': 'Maybe they have a population, which can apparently be above 4 billion.', 'start': 230.96, 'duration': 3.883}], 'summary': 'Use unordered map for speed unless needing sorted data; city record with name and population above 4 billion.', 'duration': 24.837, 'max_score': 210.006, 'thumbnail': 'https://coursnap.oss-ap-southeast-1.aliyuncs.com/video-capture/KiB0vRi2wlc/pics/KiB0vRi2wlc210006.jpg'}, {'end': 501.122, 'src': 'heatmap', 'start': 468.16, 'weight': 0.797, 'content': [{'end': 469.061, 'text': "There's my population.", 'start': 468.16, 'duration': 0.901}, {'end': 472.643, 'text': "How easy was that? And again, it's not just easy, it's really fast.", 'start': 469.141, 'duration': 3.502}, {'end': 479.607, 'text': 'For a small data set like this, it probably is about the same speed, but the larger your data set is, the faster this will essentially be.', 'start': 472.763, 'duration': 6.844}, {'end': 481.869, 'text': 'This has the added bonus of being ordered.', 'start': 479.727, 'duration': 2.142}, {'end': 486.312, 'text': 'So if I was to iterate through all of these, then these cities would actually be in alphabetical order.', 'start': 482.109, 'duration': 4.203}, {'end': 488.933, 'text': 'But Cherno, you said an unordered map is faster, maybe.', 'start': 486.392, 'duration': 2.541}, {'end': 495.157, 'text': "How do you use an unordered map? Well, you'll be delighted to know that it's the same, but you just change the type to unordered map.", 'start': 489.334, 'duration': 5.823}, {'end': 496.098, 'text': 'The API is identical.', 'start': 495.177, 'duration': 0.921}, {'end': 501.122, 'text': "All right, that's pretty much the video, but you know me, I'm gonna keep talking for like another 20 minutes probably.", 'start': 496.178, 'duration': 4.944}], 'summary': 'Demonstrates use of ordered and unordered maps for faster data processing.', 'duration': 32.962, 'max_score': 468.16, 'thumbnail': 'https://coursnap.oss-ap-southeast-1.aliyuncs.com/video-capture/KiB0vRi2wlc/pics/KiB0vRi2wlc468160.jpg'}, {'end': 543.162, 'src': 'embed', 'start': 517.756, 'weight': 2, 'content': [{'end': 522.419, 'text': "The best thing about Hostinger is that it's extremely affordable, but it doesn't sacrifice quality.", 'start': 517.756, 'duration': 4.663}, {'end': 524.642, 'text': 'Their servers are really reliable,', 'start': 522.6, 'duration': 2.042}, {'end': 530.948, 'text': 'their user interfaces are fantastic and they also have a bunch of well-written documentation and tutorials to help you get started.', 'start': 524.642, 'duration': 6.306}, {'end': 534.352, 'text': "The process of setting up a brand new server is you're watching it right now.", 'start': 531.008, 'duration': 3.344}, {'end': 535.553, 'text': "It's really simple.", 'start': 534.612, 'duration': 0.941}, {'end': 537.756, 'text': "I probably can't even finish this sentence in time.", 'start': 535.974, 'duration': 1.782}, {'end': 539.618, 'text': "Yeah, there's like nothing more to say.", 'start': 538.016, 'duration': 1.602}, {'end': 541.78, 'text': 'If you need a web host, then just go to Hosinger.', 'start': 539.838, 'duration': 1.942}, {'end': 543.162, 'text': "That's Hosinger.com slash Cherno.", 'start': 541.82, 'duration': 1.342}], 'summary': 'Hostinger is affordable, reliable, and user-friendly with helpful documentation. setting up a new server is simple and quick.', 'duration': 25.406, 'max_score': 517.756, 'thumbnail': 'https://coursnap.oss-ap-southeast-1.aliyuncs.com/video-capture/KiB0vRi2wlc/pics/KiB0vRi2wlc517756.jpg'}], 'start': 0.049, 'title': 'Using c++ maps for efficient data retrieval', 'summary': 'Covers the concept of maps in c++, comparing map and unordered map, and discusses the advantages of using maps for data retrieval, including a fictional scenario. it also highlights the inefficiency of using a vector for search and introduces the benefits of using a map for speed and efficiency, with a mention of hostinger for web hosting.', 'chapters': [{'end': 286.052, 'start': 0.049, 'title': 'C++ maps overview', 'summary': 'Explores the concept of maps in c++, covering the difference between map and unordered map, their advantages, and a fictional scenario demonstrating their usage for city records.', 'duration': 286.003, 'highlights': ['Maps in C++ refer to a data structure that allows the association of a key with a value, serving as a solution to the problem of retrieving specific elements from a vector. The concept of maps in C++ allows for the association of a key with a value, solving the problem of retrieving specific elements from a vector by providing a more efficient way to access data.', 'Two different types of maps in C++ are discussed - map and unordered map, with the former being a self-balancing binary search tree and the latter being a hash table. The chapter delves into two different types of maps in C++, namely map and unordered map, where map is a self-balancing binary search tree and unordered map is a hash table, each with its unique characteristics and applications.', 'The recommendation to use unordered map over map is provided, suggesting its faster performance in most scenarios unless a sorted data structure is specifically required. A recommendation to prioritize the usage of unordered map over map is given, indicating that unordered map usually offers faster performance unless a sorted data structure is essential for the particular use case.']}, {'end': 550.77, 'start': 286.273, 'title': 'Optimizing data retrieval with maps', 'summary': 'Discusses the inefficiency of using a vector to search for specific data and the advantages of using a map instead, highlighting the benefits of speed and efficiency, and the use of hostinger for web hosting.', 'duration': 264.497, 'highlights': ['Using a map for data retrieval is more efficient than a vector, providing faster and easier access to specific information. The chapter discusses the inefficiency of using a vector to search for specific data and the advantages of using a map instead, highlighting the benefits of speed and efficiency.', "The use of Hostinger for web hosting is recommended due to its affordability, reliability, and user-friendly interface. The best thing about Hostinger is that it's extremely affordable, but it doesn't sacrifice quality. Their servers are really reliable, their user interfaces are fantastic and they also have a bunch of well-written documentation and tutorials to help you get started."]}], 'duration': 550.721, 'thumbnail': 'https://coursnap.oss-ap-southeast-1.aliyuncs.com/video-capture/KiB0vRi2wlc/pics/KiB0vRi2wlc49.jpg', 'highlights': ['The recommendation to use unordered map over map is provided, suggesting its faster performance in most scenarios unless a sorted data structure is specifically required.', 'Using a map for data retrieval is more efficient than a vector, providing faster and easier access to specific information.', 'The use of Hostinger for web hosting is recommended due to its affordability, reliability, and user-friendly interface.', 'The concept of maps in C++ allows for the association of a key with a value, solving the problem of retrieving specific elements from a vector by providing a more efficient way to access data.', 'Two different types of maps in C++ are discussed - map and unordered map, with the former being a self-balancing binary search tree and the latter being a hash table.']}, {'end': 749.11, 'segs': [{'end': 596.615, 'src': 'heatmap', 'start': 551.511, 'weight': 0, 'content': [{'end': 554.435, 'text': "Maps Let's dive in and take a look at maps in much more detail.", 'start': 551.511, 'duration': 2.924}, {'end': 558.399, 'text': "Most of what I'm going to explain here applies to both unordered map and map.", 'start': 554.535, 'duration': 3.864}, {'end': 563.204, 'text': "Obviously, there's specific stuff I will mention, but as I mentioned, the API is very similar.", 'start': 558.479, 'duration': 4.725}, {'end': 566.348, 'text': "So first of all, let's discuss this in a bit more detail.", 'start': 563.305, 'duration': 3.043}, {'end': 569.62, 'text': 'So again, we have a key, obviously an STD string.', 'start': 566.778, 'duration': 2.842}, {'end': 576.966, 'text': "Now it's really important, first and foremost, that whatever you use as the key is actually hashable if you're using an unordered map,", 'start': 569.94, 'duration': 7.026}, {'end': 579.808, 'text': "or it has less than operator if you're using an STD map.", 'start': 576.966, 'duration': 2.842}, {'end': 586.591, 'text': "If we decided that we actually wanted, for example, to be like well, let's create a map of city record to.", 'start': 579.868, 'duration': 6.723}, {'end': 589.232, 'text': "I don't know some additional data that we wanted.", 'start': 586.591, 'duration': 2.641}, {'end': 596.615, 'text': "maybe that wasn't part of this original struct, such as like I don't know the year that it was like, founded in or something like that.", 'start': 589.232, 'duration': 7.383}], 'summary': 'Detailed explanation of maps, emphasizing key requirements for keys and similar apis for unordered map and map.', 'duration': 37.721, 'max_score': 551.511, 'thumbnail': 'https://coursnap.oss-ap-southeast-1.aliyuncs.com/video-capture/KiB0vRi2wlc/pics/KiB0vRi2wlc551511.jpg'}, {'end': 672.516, 'src': 'embed', 'start': 648.464, 'weight': 1, 'content': [{'end': 655.007, 'text': "So if you actually follow this through, you'll see that eventually you get to message C reference to class template instantiation.", 'start': 648.464, 'duration': 6.543}, {'end': 659.489, 'text': 'And if we double click on this, it will take us first of all to where we are, which, as you can see,', 'start': 655.027, 'duration': 4.462}, {'end': 662.471, 'text': 'is that founded map that I tried to create really important.', 'start': 659.489, 'duration': 2.982}, {'end': 663.683, 'text': 'that you look at the output.', 'start': 662.742, 'duration': 0.941}, {'end': 667.188, 'text': "Now it's telling you here, by the way, all of the template arguments.", 'start': 663.783, 'duration': 3.405}, {'end': 670.833, 'text': "So we know that for example, it's trying to use a city hash city record.", 'start': 667.429, 'duration': 3.404}, {'end': 672.516, 'text': "That doesn't exist.", 'start': 671.574, 'duration': 0.942}], 'summary': 'Explanation of message c reference to class template instantiation and template arguments.', 'duration': 24.052, 'max_score': 648.464, 'thumbnail': 'https://coursnap.oss-ap-southeast-1.aliyuncs.com/video-capture/KiB0vRi2wlc/pics/KiB0vRi2wlc648464.jpg'}, {'end': 713.355, 'src': 'embed', 'start': 683.366, 'weight': 4, 'content': [{'end': 687.411, 'text': 'Again, everything goes through that hash function, as I mentioned, but city record is a custom type.', 'start': 683.366, 'duration': 4.045}, {'end': 690.053, 'text': 'So how is C++ supposed to know how to hash this?', 'start': 687.771, 'duration': 2.282}, {'end': 696.34, 'text': 'Now, as a side note, if this was, for example, a city record pointer, which again a lot of people like to use,', 'start': 690.614, 'duration': 5.726}, {'end': 701.03, 'text': "and if you're coming from a managed language, then Basically everything is usually a pointer there.", 'start': 696.34, 'duration': 4.69}, {'end': 702.311, 'text': 'That is a different story.', 'start': 701.29, 'duration': 1.021}, {'end': 704.371, 'text': 'Why? Because a pointer is just an integer.', 'start': 702.471, 'duration': 1.9}, {'end': 707.753, 'text': "It's a 64-bit integer on a 64-bit compiled binary.", 'start': 704.512, 'duration': 3.241}, {'end': 713.355, 'text': "So it's obviously going to be able to hash that because you may as well have written a uint64t.", 'start': 708.153, 'duration': 5.202}], 'summary': 'In c++, a city record pointer is a 64-bit integer that can be hashed easily.', 'duration': 29.989, 'max_score': 683.366, 'thumbnail': 'https://coursnap.oss-ap-southeast-1.aliyuncs.com/video-capture/KiB0vRi2wlc/pics/KiB0vRi2wlc683366.jpg'}, {'end': 760.17, 'src': 'embed', 'start': 735.718, 'weight': 5, 'content': [{'end': 742.183, 'text': "This is one of the things that I pretty much Google anytime I have to implement because I don't remember how to do this off the top of my head.", 'start': 735.718, 'duration': 6.465}, {'end': 745.766, 'text': "But I have already worked out how to do this for the purpose of this video, so I'll show you how to do it.", 'start': 742.303, 'duration': 3.463}, {'end': 749.11, 'text': "But yeah, programmers Google stuff all the time, and that's I mean that's pretty obvious, right?", 'start': 745.826, 'duration': 3.284}, {'end': 752.813, 'text': 'Now, theoretically, if we wanted to, we could kind of reverse engineer this,', 'start': 749.21, 'duration': 3.603}, {'end': 760.17, 'text': "because we know that it's basically going to come up to here and try and use hash, but it's like Again,", 'start': 752.813, 'duration': 7.357}], 'summary': 'Programmers frequently google for implementation details, as demonstrated in this video.', 'duration': 24.452, 'max_score': 735.718, 'thumbnail': 'https://coursnap.oss-ap-southeast-1.aliyuncs.com/video-capture/KiB0vRi2wlc/pics/KiB0vRi2wlc735718.jpg'}], 'start': 551.511, 'title': 'Maps in c++', 'summary': 'Covers detailed usage of maps in c++, emphasizing the importance of hashable keys and discussing similarities between unordered map and map. it also discusses the challenges of debugging c++ and maps, providing insight into specific error messages, and explains custom type hashing in c++, highlighting the need for hash functions for non-pointer custom types.', 'chapters': [{'end': 609.791, 'start': 551.511, 'title': 'Maps in c++: detailed overview', 'summary': 'Covers the detailed usage of maps in c++, emphasizing the importance of using hashable keys and discussing the similarities between unordered map and map.', 'duration': 58.28, 'highlights': ["It's important that the key used in maps is hashable for unordered map, or has a less than operator for STD map.", 'The API for unordered map and map is very similar, with specific differences mentioned.', 'The chapter emphasizes the need for hashable keys when using unordered maps or the presence of a less than operator when using STD maps.']}, {'end': 667.188, 'start': 610.132, 'title': 'Debugging c++ maps', 'summary': 'Discusses the challenges of debugging c++ and maps, emphasizing the importance of looking at the output for detailed error messages and providing insight into a specific error message related to class template instantiation in c++.', 'duration': 57.056, 'highlights': ['It is important to look at the output for detailed error messages instead of relying solely on the error list, as the output provides much more detail about the errors.', 'Understanding the specific error message, such as the reference to class template instantiation, can help in debugging C++ code more effectively.']}, {'end': 749.11, 'start': 667.429, 'title': 'Custom type hashing in c++', 'summary': 'Explains the challenges of hashing a custom type in c++, highlighting the need to provide a hash function for non-pointer custom types, as opposed to easily hashable pointer types, and the common practice of programmers googling for such implementations.', 'duration': 81.681, 'highlights': ['Custom type city record in C++ requires a specific hash function to be provided, unlike pointer types, which are easily hashable due to their representation as 64-bit integers.', 'Programmers commonly resort to searching for implementations of custom type hash functions, highlighting the practical nature of finding solutions online.', 'The challenge of hashing a custom type in C++ is emphasized through the need to provide a hash function for non-pointer custom types, as opposed to pointer types, which are easily hashable due to their representation as 64-bit integers.']}], 'duration': 197.599, 'thumbnail': 'https://coursnap.oss-ap-southeast-1.aliyuncs.com/video-capture/KiB0vRi2wlc/pics/KiB0vRi2wlc551511.jpg', 'highlights': ['The API for unordered map and map is very similar, with specific differences mentioned.', 'Understanding the specific error message, such as the reference to class template instantiation, can help in debugging C++ code more effectively.', "It's important that the key used in maps is hashable for unordered map, or has a less than operator for STD map.", 'The chapter emphasizes the need for hashable keys when using unordered maps or the presence of a less than operator when using STD maps.', 'Custom type city record in C++ requires a specific hash function to be provided, unlike pointer types, which are easily hashable due to their representation as 64-bit integers.', 'Programmers commonly resort to searching for implementations of custom type hash functions, highlighting the practical nature of finding solutions online.', 'The challenge of hashing a custom type in C++ is emphasized through the need to provide a hash function for non-pointer custom types, as opposed to pointer types, which are easily hashable due to their representation as 64-bit integers.']}, {'end': 995.031, 'segs': [{'end': 801.849, 'src': 'embed', 'start': 772.694, 'weight': 0, 'content': [{'end': 777.615, 'text': 'So normally we would implement it like this, namespace std, because this has to be in the std namespace.', 'start': 772.694, 'duration': 4.921}, {'end': 782.177, 'text': "And then we're going to create a template specialization for the hash struct.", 'start': 777.815, 'duration': 4.362}, {'end': 785.518, 'text': "Now this hash struct again, because it's a template specialization.", 'start': 782.577, 'duration': 2.941}, {'end': 791.502, 'text': "we're going to type in template with an empty template argument and then we're going to specify the template argument here.", 'start': 785.518, 'duration': 5.984}, {'end': 794.784, 'text': "so of course, hash of city record is what we'll be creating.", 'start': 791.502, 'duration': 3.282}, {'end': 801.849, 'text': 'and then that comes with an operator, a parentheses operator, which is like the call operator that returns a size t,', 'start': 794.784, 'duration': 7.065}], 'summary': 'Implement a template specialization for hash struct in the std namespace for city record to return a size t.', 'duration': 29.155, 'max_score': 772.694, 'thumbnail': 'https://coursnap.oss-ap-southeast-1.aliyuncs.com/video-capture/KiB0vRi2wlc/pics/KiB0vRi2wlc772694.jpg'}, {'end': 843.174, 'src': 'embed', 'start': 812.199, 'weight': 1, 'content': [{'end': 815.782, 'text': "And then it's up to us to return any kind of hash value that we want.", 'start': 812.199, 'duration': 3.583}, {'end': 823.029, 'text': 'Now, we could just return zero and then obviously all city records are now equal because whenever you hash a city record, you always get zero.', 'start': 815.822, 'duration': 7.207}, {'end': 824.23, 'text': "So we don't want to do that.", 'start': 823.35, 'duration': 0.88}, {'end': 831.172, 'text': "Instead, we want to maybe take a look at the data and be like, what uniquely identifies this? Now, cities don't really have unique names.", 'start': 824.31, 'duration': 6.862}, {'end': 836.153, 'text': "I mean, there are many cities in the world, especially if you don't just look at like capital cities, obviously, but you go further down.", 'start': 831.352, 'duration': 4.801}, {'end': 843.174, 'text': 'So you could, like you know, hash the name with the population and combine that and latitude and longitude.', 'start': 837.053, 'duration': 6.121}], 'summary': 'To create unique hash values for cities, consider combining name, population, latitude, and longitude.', 'duration': 30.975, 'max_score': 812.199, 'thumbnail': 'https://coursnap.oss-ap-southeast-1.aliyuncs.com/video-capture/KiB0vRi2wlc/pics/KiB0vRi2wlc812199.jpg'}, {'end': 975.63, 'src': 'embed', 'start': 943.996, 'weight': 2, 'content': [{'end': 949.22, 'text': "So the reason this didn't compile here is because I'm not sure why it just decided to break now.", 'start': 943.996, 'duration': 5.224}, {'end': 953.703, 'text': 'Maybe it never worked, but basically I used in place back with four arguments here for the vector.', 'start': 949.36, 'duration': 4.343}, {'end': 957.482, 'text': "we don't, technically speaking, have a constructor that matches that.", 'start': 954.321, 'duration': 3.161}, {'end': 958.683, 'text': "i was hoping that it wouldn't matter.", 'start': 957.482, 'duration': 1.201}, {'end': 962.484, 'text': "but anyway, since we're past vectors, if you wanted to make this work obviously within place back,", 'start': 958.683, 'duration': 3.801}, {'end': 967.506, 'text': 'you would just write a constructor that took in all those arguments instead of kind of leaving up to the struct initializer list.', 'start': 962.484, 'duration': 5.022}, {'end': 975.63, 'text': 'but we are going to just get rid of our vector ctrl f7, and there we go, it can pass and everything is wonderful now.', 'start': 967.506, 'duration': 8.124}], 'summary': 'The code did not compile due to constructor mismatch, but after removing the vector, it passed successfully.', 'duration': 31.634, 'max_score': 943.996, 'thumbnail': 'https://coursnap.oss-ap-southeast-1.aliyuncs.com/video-capture/KiB0vRi2wlc/pics/KiB0vRi2wlc943996.jpg'}], 'start': 749.21, 'title': 'Custom hashing in c++', 'summary': 'Discusses creating a template specialization for the std hash class in c++ to define custom hashing for a specific type, emphasizing the need for uniqueness to avoid data overwriting and addressing compilation issues related to vector usage.', 'chapters': [{'end': 995.031, 'start': 749.21, 'title': 'Custom hashing in c++', 'summary': 'Discusses creating a template specialization for the std hash class in c++ to define custom hashing for a specific type, emphasizing the need for uniqueness to avoid data overwriting and addressing compilation issues related to vector usage.', 'duration': 245.821, 'highlights': ['Need for Unique Hashing Emphasizes the importance of creating a unique hash to avoid data overwriting, highlighting the potential risk if the hash values are not unique.', 'Template Specialization for std hash class Explains the process of creating a template specialization for the std hash class in C++ for a specific type, using the example of creating a hash for city records.', 'Addressing Compilation Issue with Vector Usage Addresses a compilation issue related to using in place back with four arguments for a vector, highlighting the need to either create a constructor that takes all the arguments or modify the usage to resolve the error.']}], 'duration': 245.821, 'thumbnail': 'https://coursnap.oss-ap-southeast-1.aliyuncs.com/video-capture/KiB0vRi2wlc/pics/KiB0vRi2wlc749210.jpg', 'highlights': ['Template Specialization for std hash class: Explains the process of creating a template specialization for the std hash class in C++ for a specific type, using the example of creating a hash for city records.', 'Need for Unique Hashing: Emphasizes the importance of creating a unique hash to avoid data overwriting, highlighting the potential risk if the hash values are not unique.', 'Addressing Compilation Issue with Vector Usage: Addresses a compilation issue related to using in place back with four arguments for a vector, highlighting the need to either create a constructor that takes all the arguments or modify the usage to resolve the error.']}, {'end': 1269.423, 'segs': [{'end': 1045.079, 'src': 'embed', 'start': 1019.111, 'weight': 4, 'content': [{'end': 1025.546, 'text': 'because this index operator that you use to insert things in will always insert things in.', 'start': 1019.111, 'duration': 6.435}, {'end': 1028.267, 'text': 'There is no const version of this operator.', 'start': 1025.586, 'duration': 2.681}, {'end': 1032.289, 'text': "What am I talking about? Well, let's get rid of this founded map because it's a little bit annoying.", 'start': 1028.727, 'duration': 3.562}, {'end': 1045.079, 'text': "If I wanted to look up Berlin, like so, if Berlin didn't actually exist, Then this code here actually will insert Berlin into the unordered map.", 'start': 1033.108, 'duration': 11.971}], 'summary': 'The index operator always inserts items into the unordered map, with no const version available.', 'duration': 25.968, 'max_score': 1019.111, 'thumbnail': 'https://coursnap.oss-ap-southeast-1.aliyuncs.com/video-capture/KiB0vRi2wlc/pics/KiB0vRi2wlc1019111.jpg'}, {'end': 1155.684, 'src': 'embed', 'start': 1127.024, 'weight': 3, 'content': [{'end': 1131.847, 'text': 'So I actually tend to use this quite a lot to just avoid copies and to make the code a little bit cleaner.', 'start': 1127.024, 'duration': 4.823}, {'end': 1138.453, 'text': 'But if I really, truly want to retrieve the data without actually inserting it, then what I should be using is dot at.', 'start': 1131.887, 'duration': 6.566}, {'end': 1141.975, 'text': 'Now, as a quick little note before I show you .', 'start': 1139.073, 'duration': 2.902}, {'end': 1147.078, 'text': "at, since this index operator inserts elements if they're not there.", 'start': 1141.975, 'duration': 5.103}, {'end': 1155.684, 'text': 'if you happen to have like a const map, so if I wrote something like const, auto cities equals city map.', 'start': 1147.078, 'duration': 8.606}], 'summary': 'Using dot at to retrieve data without inserting it, to avoid copies and make code cleaner.', 'duration': 28.66, 'max_score': 1127.024, 'thumbnail': 'https://coursnap.oss-ap-southeast-1.aliyuncs.com/video-capture/KiB0vRi2wlc/pics/KiB0vRi2wlc1127024.jpg'}, {'end': 1212.27, 'src': 'embed', 'start': 1172.155, 'weight': 1, 'content': [{'end': 1180.462, 'text': "I remember back in the day I was so confused as to why I couldn't use index operators If the map was const.", 'start': 1172.155, 'duration': 8.307}, {'end': 1184.625, 'text': "I'm like, but I'm just trying to access because like in Java and C sharp, whatever, this is normal.", 'start': 1180.462, 'duration': 4.163}, {'end': 1193.831, 'text': 'Right But in C++ that operator is kind of like, it has the ability to mutate the actual data, of course, the map, because it can insert elements.', 'start': 1184.825, 'duration': 9.006}, {'end': 1195.932, 'text': "So of course there can't be a constant.", 'start': 1194.391, 'duration': 1.541}, {'end': 1201.956, 'text': 'They could have made a constant version that would not insert elements, but I guess to keep it less confusing, they made dot app instead.', 'start': 1195.972, 'duration': 5.984}, {'end': 1205.406, 'text': 'So dot at is in fact const because it will never insert new elements.', 'start': 1202.324, 'duration': 3.082}, {'end': 1207.927, 'text': 'It will only return those elements to you.', 'start': 1205.786, 'duration': 2.141}, {'end': 1212.27, 'text': "Now, of course, it will return them to you in a const state, which is why that wasn't working.", 'start': 1208.227, 'duration': 4.043}], 'summary': "In c++, using 'dot at' is const, it will never insert new elements.", 'duration': 40.115, 'max_score': 1172.155, 'thumbnail': 'https://coursnap.oss-ap-southeast-1.aliyuncs.com/video-capture/KiB0vRi2wlc/pics/KiB0vRi2wlc1172155.jpg'}, {'end': 1258.017, 'src': 'embed', 'start': 1230.2, 'weight': 0, 'content': [{'end': 1233.002, 'text': "Now, what happens if the data isn't there? So we don't have Berlin here.", 'start': 1230.2, 'duration': 2.802}, {'end': 1235.523, 'text': "So what's going to happen here? Well, you're going to crash.", 'start': 1233.302, 'duration': 2.221}, {'end': 1238.545, 'text': "It's going to like throw an exception or something or assert.", 'start': 1235.703, 'duration': 2.842}, {'end': 1239.206, 'text': 'If you use .', 'start': 1238.705, 'duration': 0.501}, {'end': 1240.867, 'text': 'at, the data has to be there.', 'start': 1239.206, 'duration': 1.661}, {'end': 1245.289, 'text': "So how does one check to see if the data is there? That's where we can use .", 'start': 1240.927, 'duration': 4.362}, {'end': 1247.17, 'text': "find And again, it's pretty simple.", 'start': 1245.289, 'duration': 1.881}, {'end': 1253.414, 'text': "You can just write cities.find Berlin, which is my key, doesn't equal cities.end.", 'start': 1247.511, 'duration': 5.903}, {'end': 1258.017, 'text': "I don't know why my new Visual Studio decides to convert that.", 'start': 1253.434, 'duration': 4.583}], 'summary': "Using find function to check if data is present, like cities.find berlin, which is my key, doesn't equal cities.end.", 'duration': 27.817, 'max_score': 1230.2, 'thumbnail': 'https://coursnap.oss-ap-southeast-1.aliyuncs.com/video-capture/KiB0vRi2wlc/pics/KiB0vRi2wlc1230200.jpg'}], 'start': 995.458, 'title': 'C++ map operations', 'summary': 'Covers c++ unordered map insertion and retrieval, emphasizing efficient code and discusses understanding c++ map constants, highlighting limitations and necessity of using dot find.', 'chapters': [{'end': 1147.078, 'start': 995.458, 'title': 'C++ unordered map insertion and retrieval', 'summary': 'Covers the usage of the index operator in unordered map in c++, detailing how it inserts elements and allows retrieval, emphasizing on avoiding copies and ensuring efficient code.', 'duration': 151.62, 'highlights': ['The index operator in C++ unordered map always inserts elements and does not have a const version, allowing insertion and retrieval without specifying parameters. The index operator in C++ unordered map always inserts elements and does not have a const version, allowing insertion and retrieval without specifying parameters.', 'Using the index operator to insert elements in C++ unordered map provides a reference to the newly inserted record, allowing access to the memory without the need for later assignment. Using the index operator to insert elements in C++ unordered map provides a reference to the newly inserted record, allowing access to the memory without the need for later assignment.', 'Using the dot at method in C++ unordered map allows retrieval of data without inserting it, providing a way to access elements without modifying the map. Using the dot at method in C++ unordered map allows retrieval of data without inserting it, providing a way to access elements without modifying the map.']}, {'end': 1269.423, 'start': 1147.078, 'title': 'Understanding c++ map constants', 'summary': 'Discusses the usage of const maps in c++, highlighting the limitations of the index operator, the use of dot at to access elements in a const state, and the necessity of using dot find to check for the existence of data in a map.', 'duration': 122.345, 'highlights': ['The index operator cannot be used with a const map in C++, leading to the necessity of using dot at to access elements in a const state.', 'Dot at in C++ maps is const and will only return elements in a const state without inserting new elements.', 'The usage of dot find in C++ maps enables checking for the existence of data before accessing it, preventing crashes or exceptions.']}], 'duration': 273.965, 'thumbnail': 'https://coursnap.oss-ap-southeast-1.aliyuncs.com/video-capture/KiB0vRi2wlc/pics/KiB0vRi2wlc995458.jpg', 'highlights': ['Using the dot find in C++ maps enables checking for the existence of data before accessing it, preventing crashes or exceptions.', 'The usage of dot at in C++ maps is const and will only return elements in a const state without inserting new elements.', 'The index operator cannot be used with a const map in C++, leading to the necessity of using dot at to access elements in a const state.', 'Using the dot at method in C++ unordered map allows retrieval of data without inserting it, providing a way to access elements without modifying the map.', 'Using the index operator to insert elements in C++ unordered map provides a reference to the newly inserted record, allowing access to the memory without the need for later assignment.', 'The index operator in C++ unordered map always inserts elements and does not have a const version, allowing insertion and retrieval without specifying parameters.']}, {'end': 1780.858, 'segs': [{'end': 1310.935, 'src': 'embed', 'start': 1286.848, 'weight': 4, 'content': [{'end': 1293.29, 'text': "Ideally, yes, you wouldn't iterate through maps because a vector keeps all of your elements in contiguous memory, very cache friendly.", 'start': 1286.848, 'duration': 6.442}, {'end': 1296.53, 'text': "It's just, it's the best thing to do if you want to iterate through elements.", 'start': 1293.43, 'duration': 3.1}, {'end': 1301.211, 'text': 'That is why vectors in general are by far like the most popular data structure.', 'start': 1296.931, 'duration': 4.28}, {'end': 1303.292, 'text': 'Most of the time you use a vector.', 'start': 1301.472, 'duration': 1.82}, {'end': 1310.935, 'text': "because it just stores all your memory in a row and you probably don't need fancy indexing systems like this most of the time.", 'start': 1303.672, 'duration': 7.263}], 'summary': 'Vectors are ideal for iteration, popular due to contiguous memory and cache-friendly nature.', 'duration': 24.087, 'max_score': 1286.848, 'thumbnail': 'https://coursnap.oss-ap-southeast-1.aliyuncs.com/video-capture/KiB0vRi2wlc/pics/KiB0vRi2wlc1286848.jpg'}, {'end': 1362.942, 'src': 'embed', 'start': 1339.845, 'weight': 0, 'content': [{'end': 1347.969, 'text': 'and then using structured bindings in C++ 17, I can actually just write like the name of my key, such as like the name of the city,', 'start': 1339.845, 'duration': 8.124}, {'end': 1349.629, 'text': 'and then whatever value I want.', 'start': 1347.969, 'duration': 1.66}, {'end': 1353.052, 'text': 'So city, right, because we have a name to city record.', 'start': 1349.689, 'duration': 3.363}, {'end': 1354.954, 'text': 'Then I just specify my city map.', 'start': 1353.092, 'duration': 1.862}, {'end': 1357.116, 'text': "So it's kind of like a range-based for loop here.", 'start': 1354.974, 'duration': 2.142}, {'end': 1362.942, 'text': "However, we can use structured bindings, which by the way, as I compile, I realized that we haven't enabled C++ 17.", 'start': 1357.336, 'duration': 5.606}], 'summary': 'Using structured bindings in c++ 17 to simplify code, but c++ 17 needs enabling.', 'duration': 23.097, 'max_score': 1339.845, 'thumbnail': 'https://coursnap.oss-ap-southeast-1.aliyuncs.com/video-capture/KiB0vRi2wlc/pics/KiB0vRi2wlc1339845.jpg'}, {'end': 1501, 'src': 'embed', 'start': 1474.893, 'weight': 2, 'content': [{'end': 1479.858, 'text': 'so Of course, we would just use an air city map instead of that air city unordered map.', 'start': 1474.893, 'duration': 4.965}, {'end': 1481.519, 'text': "And that's really all we have to do.", 'start': 1480.018, 'duration': 1.501}, {'end': 1486.882, 'text': 'So now if I hit F5, then you can see that we now have all of these in alphabetical order.', 'start': 1481.739, 'duration': 5.143}, {'end': 1494.868, 'text': 'How do we remove one of these from the map, you ask? Why, we can simply do city map dot erase and then whatever key we want to erase.', 'start': 1487.043, 'duration': 7.825}, {'end': 1496.789, 'text': "So for example, we don't want Paris in here.", 'start': 1495.048, 'duration': 1.741}, {'end': 1501, 'text': "we just city map dot erase Paris, and that's it, no more Paris.", 'start': 1497.179, 'duration': 3.821}], 'summary': 'Using an air city map, items are now in alphabetical order. items can be removed using city map dot erase.', 'duration': 26.107, 'max_score': 1474.893, 'thumbnail': 'https://coursnap.oss-ap-southeast-1.aliyuncs.com/video-capture/KiB0vRi2wlc/pics/KiB0vRi2wlc1474893.jpg'}, {'end': 1624.569, 'src': 'heatmap', 'start': 1598.343, 'weight': 0.787, 'content': [{'end': 1602.505, 'text': 'As a quick side note, this less than operator is responsible for more than just sorting, of course.', 'start': 1598.343, 'duration': 4.162}, {'end': 1605.446, 'text': 'This is what defines the unique key inside your actual map.', 'start': 1602.565, 'duration': 2.881}, {'end': 1610.248, 'text': "So if one city's population is the same as another city's population, they'll actually clash.", 'start': 1605.786, 'duration': 4.462}, {'end': 1612.989, 'text': "So it's important that you, you know, do this properly, not just for sorting.", 'start': 1610.268, 'duration': 2.721}, {'end': 1615.37, 'text': "Let's compile it with control F7 again.", 'start': 1613.309, 'duration': 2.061}, {'end': 1617.379, 'text': 'You can see it compiles now.', 'start': 1616.277, 'duration': 1.102}, {'end': 1620.543, 'text': 'Before we add all of these into this city founded map.', 'start': 1617.499, 'duration': 3.044}, {'end': 1624.569, 'text': 'another interesting note is because this index operator inserts elements.', 'start': 1620.543, 'duration': 4.026}], 'summary': 'The less than operator defines unique keys in a map, ensuring proper sorting and preventing clashes in population data.', 'duration': 26.226, 'max_score': 1598.343, 'thumbnail': 'https://coursnap.oss-ap-southeast-1.aliyuncs.com/video-capture/KiB0vRi2wlc/pics/KiB0vRi2wlc1598343.jpg'}, {'end': 1724.609, 'src': 'embed', 'start': 1696.415, 'weight': 3, 'content': [{'end': 1703.856, 'text': 'as I mentioned, you want to basically use SCD unordered map whenever you can, if you do not care about the order.', 'start': 1696.415, 'duration': 7.441}, {'end': 1709.317, 'text': "unless you've actually benchmark profiled all of that stuff and you can see that SCD map is actually faster.", 'start': 1703.856, 'duration': 5.461}, {'end': 1713.72, 'text': 'In some compilers, I have seen that std map is faster than unordered map.', 'start': 1709.737, 'duration': 3.983}, {'end': 1717.683, 'text': 'And you also have to realize that this very much depends on how much data you have.', 'start': 1714.02, 'duration': 3.663}, {'end': 1724.609, 'text': "If you have 200 elements, 500 elements, 1000 elements inside a map, you're probably not going to see a big performance difference.", 'start': 1717.703, 'duration': 6.906}], 'summary': 'Prefer using scd unordered map, unless benchmarked, for better performance.', 'duration': 28.194, 'max_score': 1696.415, 'thumbnail': 'https://coursnap.oss-ap-southeast-1.aliyuncs.com/video-capture/KiB0vRi2wlc/pics/KiB0vRi2wlc1696415.jpg'}], 'start': 1269.523, 'title': 'Iterating through maps in c++', 'summary': 'Discusses the importance of iterating through maps in c++, highlighting differences between iterating through vectors and maps, showcasing the ease of using structured bindings in c++ 17, explaining map iteration and manipulation using structured bindings, ordered maps, and erasing elements, and discussing the performance differences between std::map and std::unordered_map.', 'chapters': [{'end': 1374.549, 'start': 1269.523, 'title': 'Iterating through maps in c++', 'summary': 'Discusses the importance of iterating through maps in c++, highlighting the differences between iterating through vectors and maps and showcasing the ease of using structured bindings in c++ 17.', 'duration': 105.026, 'highlights': ['Structured bindings in C++ 17 provide an easy way to iterate through a map, allowing for concise code and improved readability.', 'Iterating through vectors is faster than iterating through maps due to the cache-friendly nature of vectors and the contiguous memory storage.', 'While vectors are more popular for iterating through elements, maps are still extremely popular and useful for various applications in C++ programming.']}, {'end': 1542.289, 'start': 1374.549, 'title': 'C++ map iteration and manipulation', 'summary': 'Explains how to iterate through and manipulate data in a c++ map using structured bindings, ordered maps, and erasing elements, showcasing the benefits of c++17 features and comparing unordered and ordered maps.', 'duration': 167.74, 'highlights': ['The chapter emphasizes using structured bindings in C++17 and above to directly access key-value pairs in a map, providing a modern and efficient way to iterate through data. C++17 and above, structured bindings', "It highlights the process of using an ordered map to achieve alphabetical order of elements, contrasting it with the unordered map's lack of inherent ordering. Comparison between ordered and unordered maps", 'The transcript demonstrates the simple process of erasing an element from the map using the erase function, showcasing the ease of manipulating data within a C++ map. Demonstration of the erase function']}, {'end': 1780.858, 'start': 1542.669, 'title': 'C++ custom class and maps performance', 'summary': 'Explains the implementation of a less than operator within a custom class for sorting elements in a map, and discusses the performance differences between std::map and std::unordered_map, emphasizing the impact of data size on lookup and iteration speeds.', 'duration': 238.189, 'highlights': ['Implementation of Less Than Operator for Sorting The implementation of a less than operator within a custom class allows for sorting elements in a map based on specific attributes, such as population, and also defines unique keys, ensuring proper sorting and preventing clashes.', 'Performance Differences Between std::map and std::unordered_map The performance of std::unordered_map is recommended over std::map if order is not a concern, unless specific benchmarking shows std::map as faster. The impact of data size on the performance difference is highlighted, with hash map being much faster for larger data sets, while map performs better for iteration.', 'Considerations for Iteration and Lookups The recommendation to use a vector for iteration if lookups are infrequent, while suggesting the use of map, preferably unordered, for lookups and occasional iteration, with a hint towards a future video discussing performance benchmarks.']}], 'duration': 511.335, 'thumbnail': 'https://coursnap.oss-ap-southeast-1.aliyuncs.com/video-capture/KiB0vRi2wlc/pics/KiB0vRi2wlc1269523.jpg', 'highlights': ['Structured bindings in C++ 17 provide an easy way to iterate through a map, allowing for concise code and improved readability.', 'The chapter emphasizes using structured bindings in C++17 and above to directly access key-value pairs in a map, providing a modern and efficient way to iterate through data. C++17 and above, structured bindings', 'The transcript demonstrates the simple process of erasing an element from the map using the erase function, showcasing the ease of manipulating data within a C++ map. Demonstration of the erase function', 'The performance of std::unordered_map is recommended over std::map if order is not a concern, unless specific benchmarking shows std::map as faster. The impact of data size on the performance difference is highlighted, with hash map being much faster for larger data sets, while map performs better for iteration.', 'While vectors are more popular for iterating through elements, maps are still extremely popular and useful for various applications in C++ programming.']}], 'highlights': ['The performance of std::unordered_map is recommended over std::map if order is not a concern, unless specific benchmarking shows std::map as faster. The impact of data size on the performance difference is highlighted, with hash map being much faster for larger data sets, while map performs better for iteration.', 'Using a map for data retrieval is more efficient than a vector, providing faster and easier access to specific information.', 'The recommendation to use unordered map over map is provided, suggesting its faster performance in most scenarios unless a sorted data structure is specifically required.', 'The concept of maps in C++ allows for the association of a key with a value, solving the problem of retrieving specific elements from a vector by providing a more efficient way to access data.', 'The API for unordered map and map is very similar, with specific differences mentioned.', 'The chapter emphasizes using structured bindings in C++17 and above to directly access key-value pairs in a map, providing a modern and efficient way to iterate through data. C++17 and above, structured bindings', 'The transcript demonstrates the simple process of erasing an element from the map using the erase function, showcasing the ease of manipulating data within a C++ map. Demonstration of the erase function', "It's important that the key used in maps is hashable for unordered map, or has a less than operator for STD map.", 'The use of Hostinger for web hosting is recommended due to its affordability, reliability, and user-friendly interface.', 'The challenge of hashing a custom type in C++ is emphasized through the need to provide a hash function for non-pointer custom types, as opposed to pointer types, which are easily hashable due to their representation as 64-bit integers.']}