Learn C++ With Me #17 - Maps

  Рет қаралды 79,066

Tech With Tim

Tech With Tim

Күн бұрын

Пікірлер: 87
@jamesmoffitt4503
@jamesmoffitt4503 3 жыл бұрын
As a teacher (math) who’s trying to learn code, you’re an excellent teacher.
@ispiritus4454
@ispiritus4454 3 жыл бұрын
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.
@us07251
@us07251 3 жыл бұрын
I noticed that when I use integers as keys. Thanks.
@mohamedmostafak
@mohamedmostafak 3 жыл бұрын
@iSpiritus I was going to write the same comment, then I found yours. thx for the tip and that doesn't change the fact about this video, it is a very good one.
@saintboimike
@saintboimike 2 жыл бұрын
My man 👌🏾, thanks for pointing this out
@anty.
@anty. 2 жыл бұрын
in an unordered map, are there still premade indexes for the "beginning" and "end" of them?
@edapb5574
@edapb5574 2 жыл бұрын
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.
@milomadeagame
@milomadeagame Жыл бұрын
Thank you!🚗🚗
@didgerihorn
@didgerihorn 3 жыл бұрын
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; }
@gabriellabrito8134
@gabriellabrito8134 2 жыл бұрын
can u explain what did u do there? like, this type of code isnt begginers friendly
@didgerihorn
@didgerihorn 2 жыл бұрын
@@gabriellabrito8134 This is a "range based for" loop. In other languages, similar constructs are often called "foreach". It loops over all elements that are actually contained in the variable "test". In the loop body (thatbeing the part between the opening and closing braces) we use the so-called ternary operator to assign the actual frequency to "freq". The ternary operator can be interpreted as a condensed if-else-bock looks as follows: (condition) ? value_if_true : value_if_false Here, the condition is "letter.find(c) == freq.end()", that means, if the letter c (which comes from "test") doesn't already exist as a key in our map, we set it to zero. Otherwise, if it already exists, we increase its frequency by 1
@arjunganesan9138
@arjunganesan9138 Жыл бұрын
Nice One!
@vg4414
@vg4414 2 жыл бұрын
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! 💪
@Nole2701
@Nole2701 2 жыл бұрын
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.
@marioprado8789
@marioprado8789 2 жыл бұрын
Im thinking the same
@gf408
@gf408 3 жыл бұрын
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 frequency = {}; for (int i = 0; i < test.length(); ++i) ++frequency[test[i]]; for (auto itr = frequency.begin(); itr != frequency.end(); ++itr) cout first
@vegansynths7757
@vegansynths7757 2 жыл бұрын
This is the best video on KZbin explaining maps. Great vid.
@tanvirhasanmonir1627
@tanvirhasanmonir1627 2 жыл бұрын
Wow! wonderful explanation of map. And the last coding example is really worth it to understand real life use case of map.
@torishi82
@torishi82 7 ай бұрын
Excellent! So precise and to the point. Thank you.
@waqaspathan3337
@waqaspathan3337 3 жыл бұрын
Tim is grinding right now!!!!!
@ketansonar8299
@ketansonar8299 3 жыл бұрын
You can use shorthand for loop for iterating on a map for (auto p : mp) { cout
@Shadow-lx9rh
@Shadow-lx9rh 3 жыл бұрын
Thank you 👍
@uHasioorr
@uHasioorr 3 жыл бұрын
So map, in other programing languages, is a dictionary.
@Abdullah_hassan_88
@Abdullah_hassan_88 Жыл бұрын
Exactly
@abrarmasumabir3809
@abrarmasumabir3809 3 жыл бұрын
Thanks bro Tim ...for everything.....
@alexchen4442
@alexchen4442 2 жыл бұрын
Such a good tutorial.
@shubhrajit2117
@shubhrajit2117 Жыл бұрын
An easier way for iterating through map: for (auto i : mp) cout
@jurian8620
@jurian8620 3 жыл бұрын
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.
@Iffythegreat
@Iffythegreat 3 жыл бұрын
I think this happens because the map automatically initializes and adds the pair to itself if you try to access it, the only thing is that its set to its default initialization value 0. The reason as to why it is done this way doesnt really make sense to me, I get returning the default value but it getting added to the map seems like a potential flaw
@samsularefinimon2796
@samsularefinimon2796 2 жыл бұрын
Appreciate your hard work ! 🙂
@curiositymaarouf1880
@curiositymaarouf1880 Жыл бұрын
Nice, really nice 👍
@paul-cl3el
@paul-cl3el 2 жыл бұрын
algo expert da best !!
@karjun4644
@karjun4644 3 жыл бұрын
Will I ever get a chance to see tim with long hairs before I die ?
@mr.erikchun5863
@mr.erikchun5863 Жыл бұрын
you can see his long pubic hairs
@mhb12x80
@mhb12x80 Жыл бұрын
Great job dude
@jorgealfredojaimesteheran
@jorgealfredojaimesteheran 3 жыл бұрын
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
@TheBookOfRon
@TheBookOfRon 2 жыл бұрын
You have great teaching skills. You explain every detail. Nice video!
@古川範和
@古川範和 Жыл бұрын
I got confused at the last example. The map "freq" was only declared, not initialized. How does it have the keys and values?
@SohanSharad
@SohanSharad 3 жыл бұрын
Hey so, was looking if you were going to go into package installers and how to use them, like pip or npm
@ancientelevator9
@ancientelevator9 Жыл бұрын
When you say that you aren't sure if you need the parentheses are you just saying you are not sure if it is a function/method (getter) or an actual persisted property/value of the object? (as it would be in other languages) or is there a specific C++ syntax that I am missing here?
@cantstandya3761
@cantstandya3761 2 жыл бұрын
Thanks for the good explanation.
@SunilNerella
@SunilNerella 3 жыл бұрын
Hello Tim, For maps I understand that we can create key and value... But for the line `map`, can we add one more data type to it like `map`?
@memoryLeak-CODE
@memoryLeak-CODE Жыл бұрын
Great video!!!
@clockwerkz
@clockwerkz 3 жыл бұрын
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)..
@ВикторЕфименко-й1ъ
@ВикторЕфименко-й1ъ 2 жыл бұрын
This lesson really helped me a lot. Thank you very much!
@us07251
@us07251 3 жыл бұрын
Instead of using if else statement to cout a string letters frequecny, you can write freq[letter]++
@pamp3657
@pamp3657 2 жыл бұрын
good video thank you
@priyanshusrivastava2145
@priyanshusrivastava2145 2 жыл бұрын
loved it!!
@ayushsingh-gl6wm
@ayushsingh-gl6wm 3 жыл бұрын
From where do you learn these. Like which Udemy(or any) course you took. And which platform you mostly follow for learning, and taking new courses.
@Grepehu
@Grepehu 3 жыл бұрын
I'm not sure about him but when you're learning the basic syntax of a language it's usually a lot quicker to learn by using documentation itself, some languages even give a quick written tutorial in their websites that cover all of it pretty quickly. But again, I'm not sure if it's how Tim does it.
@nijil1801
@nijil1801 2 жыл бұрын
I cant get the key when i use the value. The out put is 0. There is no syntax error. It only works one way and i dont know what to do?
@anniamatthews6803
@anniamatthews6803 2 жыл бұрын
What should I do if I want to get the max value of the map
@jonnyreh001
@jonnyreh001 2 жыл бұрын
Is this equivalent method of checking if the key is present in the map? if (freq.count(letter) == 0)
@manipurihunabopa
@manipurihunabopa Жыл бұрын
Some projects that are easy to implement in Python and other programming languages are actually difficult to implement in C++. Can you do your Python projects in C++? If you kindly do that, it will help me a lot.
@elwizko
@elwizko 3 жыл бұрын
Great lesson Tim...I have a question...can the maps be used in scenarios such as network log monitoring in incident response? I am not a pro in IR but I feel like it is possible when there are millions of logs and minimal time...I appreciate answers from all commenters.
@psymcdad8151
@psymcdad8151 2 жыл бұрын
I am trying something like this at the moment (logfile on changes in a file). Sadly map does not like std::tm as a type, so I had to convert that, basicaly making it a map of std::time_t t and std::string report. Works nice just to keep reports, but pulling data out of this is kind of a messy nightmare ^^''' ... And I have no idea how time-efficient that trick is alltogether.
@Someone-uw7je
@Someone-uw7je 3 жыл бұрын
CLASSES AND OBJECT PLEASE TIM!!
@teprox7690
@teprox7690 2 жыл бұрын
I came to the following solution: int main () { string test = "Hello world my name is tim! ttthhaaa"; map letter_freq; for (int i = 0; i < test.size(); i++) { letter_freq.insert(pair(test[i], letter_freq[test[i]]++)); } for (auto & itr : letter_freq) { cout
@pokeytradetf2582
@pokeytradetf2582 3 жыл бұрын
What happened with the private livestream??
@alexander_richter
@alexander_richter Жыл бұрын
This is plain wrong. std::map is not a hashmap, it’s a RB binary tree and it has O(log n) insertion and lookup time complexity. What you are describing is std::unordered_map.
@jodmatthewaclan5869
@jodmatthewaclan5869 3 жыл бұрын
So that's why i need to dereference my iterator, bc it returns a pointer
@Mr_Escow
@Mr_Escow 3 жыл бұрын
WOW! COOL! HI FROM RUSSIA! ))
@MarkosZorzoli
@MarkosZorzoli 2 жыл бұрын
Tnks from argentina bro
@keshav.mishra
@keshav.mishra 3 жыл бұрын
Present!
@xFlxrin69
@xFlxrin69 Жыл бұрын
nice
@Jordan4Ibanez
@Jordan4Ibanez 3 жыл бұрын
Oh very interesting. This is what I did when you typed out the string and gave the instructions: void MyOutPutTest(){ std::string test = "Hello world my name is time! ttthhaaa"; std::map frequency = {}; for (char const &thisLetter: test){ if (!frequency[thisLetter]){ //create initial value frequency[thisLetter] = 1; } else { frequency[thisLetter]++; } } //output counts std::cout
@makeitsimple7285
@makeitsimple7285 3 жыл бұрын
Please make a video about vector pls
@asadjamsheed9773
@asadjamsheed9773 3 жыл бұрын
I have a query?
@techbhanucomputer4389
@techbhanucomputer4389 3 жыл бұрын
c++ map look like a python dictionary
@mahdavimail
@mahdavimail 8 ай бұрын
Python dictionary looks like C++ maps lol.
@arctan2
@arctan2 3 жыл бұрын
console.log( ( () => 1)() + ( () => 2)() ); .......can someone explain a execution-trace on this code.
@raghavgupta6186
@raghavgupta6186 3 жыл бұрын
()=> 1 is exactly equal to function returnOne(){ return 1; } rest you can understand
@arctan2
@arctan2 3 жыл бұрын
@@raghavgupta6186 i know but which function first execute....... like which side does "+" operator see and execute.... the output is 3
@raghavgupta6186
@raghavgupta6186 3 жыл бұрын
@@arctan2 first of all both functions gets executed and 1 is returned from first function and 2 is returned from second then + operator is applied from left to right so 1 + 2 = 3
@arctan2
@arctan2 3 жыл бұрын
@@raghavgupta6186 yes i also thought the same......thanks for your time
@raghavgupta6186
@raghavgupta6186 3 жыл бұрын
@@arctan2 no problem👍
@radhekrishn702
@radhekrishn702 3 жыл бұрын
👍🏼👍🏼👍🏼👍🏼👍🏼
@alexandrucrisan5246
@alexandrucrisan5246 3 жыл бұрын
razvanon?
@treleamaryus2440
@treleamaryus2440 3 жыл бұрын
Klasna
@cyberspyking3306
@cyberspyking3306 11 ай бұрын
its getting harder 😪😮‍💨
@kevg3563
@kevg3563 2 жыл бұрын
crap video. All you did was iterate through the map. Your technique is similar to 'the 'Count Sort' algorithm. You did not explain how you can tell instantly if a value exists or not WITHOUT iterating through the map. There is, however, another technique that you could use to achieve this.
@TechWithTim
@TechWithTim 2 жыл бұрын
Well how about you share that technique? I’m always open to criticism and feedback buts it hypocritical to suggest I provided no value when you provide none yourself.
@sumukhjagirdar5860
@sumukhjagirdar5860 3 жыл бұрын
Hello Tim please check discord , I have a question for you.Just like always love the content!!!
@OviMG
@OviMG Жыл бұрын
Bro, your face expressions, your voice tone, and much more besides the appearance is much like iRaphahell (search it on youtube) Idk why but you give me same vibes😂
Learn C++ With Me #18 - Vectors
13:05
Tech With Tim
Рет қаралды 161 М.
Learn C++ With Me #16 - Tuples
17:58
Tech With Tim
Рет қаралды 31 М.
Counter-Strike 2 - Новый кс. Cтарый я
13:10
Marmok
Рет қаралды 2,8 МЛН
Sigma girl VS Sigma Error girl 2  #shorts #sigma
0:27
Jin and Hattie
Рет қаралды 124 МЛН
Maps in C++ (std::map and std::unordered_map)
30:00
The Cherno
Рет қаралды 227 М.
10 Python Concepts You NEED To Know in 2025
24:08
Tech With Tim
Рет қаралды 18 М.
Cross Platform Graphical User Interfaces in C++
44:49
javidx9
Рет қаралды 874 М.
Top 5 Passive Income Streams for Developers
12:07
Tech With Tim
Рет қаралды 5 М.
Learn C++ With Me #19 - Sets
13:33
Tech With Tim
Рет қаралды 39 М.
Learn C++ With Me #20 - Functions
26:14
Tech With Tim
Рет қаралды 57 М.
Back To Basics: C++ Containers
31:41
javidx9
Рет қаралды 190 М.
Learn Programming the Right Way...
11:02
Tech With Tim
Рет қаралды 9 М.
BEST WAY to make Desktop Applications in C++
26:00
The Cherno
Рет қаралды 944 М.
Counter-Strike 2 - Новый кс. Cтарый я
13:10
Marmok
Рет қаралды 2,8 МЛН