Learn C++ With Me #17 - Maps

Поделиться
HTML-код
  • Опубликовано: 1 янв 2025

Комментарии • 87

  • @jamesmoffitt4503
    @jamesmoffitt4503 3 года назад +23

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

  • @tanvirhasanmonir1627
    @tanvirhasanmonir1627 2 года назад +1

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

  • @torishi82
    @torishi82 8 месяцев назад

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

  • @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!
    💪

  • @didgerihorn
    @didgerihorn 3 года назад +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 года назад +2

      can u explain what did u do there? like, this type of code isnt begginers friendly

    • @didgerihorn
      @didgerihorn 2 года назад +4

      @@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!

  • @vegansynths7757
    @vegansynths7757 2 года назад

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

  • @TheBookOfRon
    @TheBookOfRon 2 года назад +1

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

  • @milomadeagame
    @milomadeagame Год назад +1

    Thank you!🚗🚗

  • @ispiritus4454
    @ispiritus4454 3 года назад +65

    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 года назад +4

      I noticed that when I use integers as keys. Thanks.

    • @mohamedmostafak
      @mohamedmostafak 3 года назад +4

      @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?

  • @abrarmasumabir3809
    @abrarmasumabir3809 3 года назад +1

    Thanks bro Tim ...for everything.....

  • @samsularefinimon2796
    @samsularefinimon2796 2 года назад +1

    Appreciate your hard work ! 🙂

  • @alexchen4442
    @alexchen4442 2 года назад +1

    Such a good tutorial.

  • @edapb5574
    @edapb5574 2 года назад +4

    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.

  • @cantstandya3761
    @cantstandya3761 2 года назад

    Thanks for the good explanation.

  • @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

  • @curiositymaarouf1880
    @curiositymaarouf1880 Год назад

    Nice, really nice 👍

  • @waqaspathan3337
    @waqaspathan3337 3 года назад +1

    Tim is grinding right now!!!!!

  • @Nole2701
    @Nole2701 2 года назад +5

    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.

  • @paul-cl3el
    @paul-cl3el 2 года назад

    algo expert da best !!

  • @ВикторЕфименко-й1ъ
    @ВикторЕфименко-й1ъ 2 года назад

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

  • @memoryLeak-CODE
    @memoryLeak-CODE Год назад

    Great video!!!

  • @priyanshusrivastava2145
    @priyanshusrivastava2145 2 года назад

    loved it!!

  • @pamp3657
    @pamp3657 2 года назад

    good video thank you

  • @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

  • @karjun4644
    @karjun4644 3 года назад +13

    Will I ever get a chance to see tim with long hairs before I die ?

  • @SohanSharad
    @SohanSharad 3 года назад +2

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

  • @uHasioorr
    @uHasioorr 3 года назад +8

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

  • @古川範和
    @古川範和 Год назад +1

    I got confused at the last example. The map "freq" was only declared, not initialized. How does it have the keys and values?

  • @jurian8620
    @jurian8620 3 года назад +1

    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

  • @SunilNerella
    @SunilNerella 3 года назад +1

    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`?

  • @clockwerkz
    @clockwerkz 3 года назад +2

    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)..

  • @ketansonar8299
    @ketansonar8299 3 года назад +5

    You can use shorthand for loop for iterating on a map
    for (auto p : mp) {
    cout

  • @shubhrajit2117
    @shubhrajit2117 Год назад +1

    An easier way for iterating through map:
    for (auto i : mp)
    cout

  • @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?

  • @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

  • @us07251
    @us07251 3 года назад

    Instead of using if else statement to cout a string letters frequecny, you can write freq[letter]++

  • @jonnyreh001
    @jonnyreh001 2 года назад

    Is this equivalent method of checking if the key is present in the map? if (freq.count(letter) == 0)

  • @Someone-uw7je
    @Someone-uw7je 3 года назад

    CLASSES AND OBJECT PLEASE TIM!!

  • @ayushsingh-gl6wm
    @ayushsingh-gl6wm 3 года назад +4

    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 года назад +1

      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.

  • @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.

  • @Mr_Escow
    @Mr_Escow 3 года назад

    WOW! COOL! HI FROM RUSSIA! ))

  • @jodmatthewaclan5869
    @jodmatthewaclan5869 3 года назад +1

    So that's why i need to dereference my iterator, bc it returns a pointer

  • @xFlxrin69
    @xFlxrin69 Год назад

    nice

  • @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.

  • @MarkosZorzoli
    @MarkosZorzoli 2 года назад

    Tnks from argentina bro

  • @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??

  • @makeitsimple7285
    @makeitsimple7285 3 года назад

    Please make a video about vector pls

  • @keshav.mishra
    @keshav.mishra 3 года назад

    Present!

  • @asadjamsheed9773
    @asadjamsheed9773 3 года назад

    I have a query?

  • @techbhanucomputer4389
    @techbhanucomputer4389 3 года назад +2

    c++ map look like a python dictionary

    • @mahdavimail
      @mahdavimail 9 месяцев назад

      Python dictionary looks like C++ maps lol.

  • @alexander_richter
    @alexander_richter Год назад +2

    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.

  • @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

  • @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 года назад +1

      @@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👍

  • @alexandrucrisan5246
    @alexandrucrisan5246 3 года назад

    razvanon?

  • @radhekrishn702
    @radhekrishn702 3 года назад +1

    👍🏼👍🏼👍🏼👍🏼👍🏼

  • @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 года назад +5

    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😂