Learn C++ With Me #17 - Maps

preview_player
Показать описание
Welcome back to another C++ tutorial! In this video, I'll be going over maps. Maps are used as another way of accessing elements and can add some efficiency to your coding.

⭐️ Timestamps ⭐️
00:00 | Intro
00:28 | What are maps
04:06 | Creating Maps
04:59 | Accessing Map Values
06:32 | Inserting Map Pairs
08:43 | Erasing Map Pairs
10:26 | Iterating Through Maps
17:02 | Practical Map Example

◼️◼️◼️◼️◼️◼️◼️◼️◼️◼️◼️◼️◼️◼️
💰 Courses & Merch 💰

🔗 Social Medias 🔗

🎬 My YouTube Gear 🎬

💸 Donations 💸
◼️◼️◼️◼️◼️◼️◼️◼️◼️◼️◼️◼️◼️◼️

⭐️ Tags ⭐️
- C++ Tutorial
- C++
- Coding Tutorial
- Tech With Tim
- Maps

⭐️ Hashtags ⭐️
#TechWithTim #C++Programming
Рекомендации по теме
Комментарии
Автор

As a teacher (math) who’s trying to learn code, you’re an excellent teacher.

jamesmoffitt
Автор

A quick correction to when you said that you can’t expect what the output is. Maps are actually ordered by key. In your case, you’ll see that the values were outputted in the lexicographical order of the char keys. There is a concept of maps that doesn’t care about order called unordered_map.

ispiritus
Автор

When counting the number of times a letter occurs, you can just use "++freq[letter]". The index operator [] automatically inserts a new element if it doesn't exist.

edapb
Автор

Thank you :) Quick performance tip: Use range based loops insted of iterators
for (const auto &c : test)
{
freq[letter] = (letter.find(c) == freq.end()) ? 0 : freq[letter] + 1;
}

didgerihorn
Автор

Wow! wonderful explanation of map. And the last coding example is really worth it to understand real life use case of map.

tanvirhasanmonir
Автор

This is the best video on YouTube explaining maps. Great vid.

vegansynths
Автор

I always hear Tim's "also" as " else so" like:
If (x) {y();} else {so();}
Is that a Canadian thing or something?
Thanks again Tim, you're out here changing lives bro!
💪

vg
Автор

Excellent! So precise and to the point. Thank you.

torishi
Автор

For the letter counter example, I don't think the if statement that checks if a letter is in the map is needed. Since the default value for ints in the map is 0, we can just start adding right away.

Nole
Автор

So people are aware since undefined keys will return a value of 0 in maps you don't actually have to check if the key exists for the practical map example.

string test = "Hello world my name is tim! ttthhaaa";

map<char, int> frequency = {};

for (int i = 0; i < test.length(); ++i)
++frequency[test[i]];

for (auto itr = frequency.begin(); itr != frequency.end(); ++itr)
cout << itr->first << " appeared " << itr->second << " times." << endl;

gf
Автор

You have great teaching skills. You explain every detail. Nice video!

TheBookOfRon
Автор

You can use shorthand for loop for iterating on a map
for (auto p : mp) {
cout << p.first << endl;
}

ketansonar
Автор

I follow you since I have memory, and I love this channel because in a unequal country like Colombia when I am from i can learn with your channel, Colombia government is KILLING us, SOS

jorgealfredojaimesteheran
Автор

An easier way for iterating through map:

for (auto i : mp)
cout << i.first << '\t' << i.second << endl;

shubhrajit
Автор

with the example at the end you don't actualy have to check if it already exists because if it doesnt exist it will equal 0 and (for some reason) if you just say ++ after a nonexistend pair it will make the pair.

jurian
Автор

Hey so, was looking if you were going to go into package installers and how to use them, like pip or npm

SohanSharad
Автор

This lesson really helped me a lot. Thank you very much!

ВикторЕфименко-йъ
Автор

So map, in other programing languages, is a dictionary.

uHasioorr
Автор

Hello Tim,
For maps I understand that we can create key and value... But for the line `map<char, int>`, can we add one more data type to it like `map<char, int, float>`?

SunilNerella
Автор

Great explanation of map! Just what I was looking for. I was curious about how it returns the map.end() if the find method doesn't contain the key one is looking for.. does this mean the map iterates through the entire structure to find the key's pointer? I'm guessing no since that would make it O(n)..

clockwerkz