Learn C++ With Me #19 - Sets

แชร์
ฝัง
  • เผยแพร่เมื่อ 1 ก.ค. 2024
  • Welcome back to another C++ tutorial! In this video, were going over sets. Sets tell us whether an element is present or not, that's the best way to describe it. I go over it more in-depth in this video.
    💻 AlgoExpert is the coding interview prep platform that I used to ace my Microsoft and Shopify interviews. Check it out and get a discount on the platform using the code "techwithtim" algoexpert.io/techwithtim
    🔍 Playlist: • Learn C++ With Me
    ⭐️ Timestamps ⭐️
    00:00 | Introduction
    00:23 | What Are Sets
    01:54 | Creating Sets
    02:43 | Iterating Through a Set
    04:11 | Inserting and Erasing Elements
    04:55 | Set Contains Element
    05:54 | Practical Set Example
    ◼️◼️◼️◼️◼️◼️◼️◼️◼️◼️◼️◼️◼️◼️
    💰 Courses & Merch 💰
    💻 The Fundamentals of Programming w/ Python: tech-with-tim.teachable.com/p...
    👕 Merchandise: teespring.com/stores/tech-wit...
    🔗 Social Medias 🔗
    📸 Instagram: / tech_with_tim
    📱 Twitter: / techwithtimm
    ⭐ Discord: / discord
    📝 LinkedIn: / tim-ruscica-82631b179
    🌎 Website: techwithtim.net
    📂 GitHub: github.com/techwithtim
    🔊 Podcast: anchor.fm/tech-with-tim
    🎬 My TH-cam Gear 🎬
    🎥 Main Camera (EOS Canon 90D): amzn.to/3cY23y9
    🎥 Secondary Camera (Panasonic Lumix G7): amzn.to/3fl2iEV
    📹 Main Lens (EFS 24mm f/2.8): amzn.to/2Yuol5r
    🕹 Tripod: amzn.to/3hpSprv
    🎤 Main Microphone (Rode NT1): amzn.to/2HrZxXc
    🎤 Secondary Microphone (Synco Wireless Lapel System): amzn.to/3e07Swl
    🎤 Third Microphone (Rode NTG4+): amzn.to/3oi0v8Z
    ☀️ Lights: amzn.to/2ApeiXr
    ⌨ Keyboard (Daskeyboard 4Q): amzn.to/2YpN5vm
    🖱 Mouse (Logitech MX Master): amzn.to/2HsmRDN
    📸 Webcam (Logitech 1080p Pro): amzn.to/2B2IXcQ
    📢 Speaker (Beats Pill): amzn.to/2XYc5ef
    🎧 Headphones (Bose Quiet Comfort 35): amzn.to/2MWbl3e
    🌞 Lamp (BenQ E-reading Lamp): amzn.to/3e0UCr8
    🌞 Secondary Lamp (BenQ Screenbar Plus): amzn.to/30Dtafi
    💻 Monitor (BenQ EX2780Q): amzn.to/2HsmUPZ
    💻 Monitor (LG Ultrawide 34WN750): amzn.to/3dSD7tS
    🎙 Mic Boom Arm (Rode PSA 1): amzn.to/30EZw9m
    🎚 Audio Interface (Focusrite Scarlet 4i4): amzn.to/2TjXsih
    💸 Donations 💸
    💵 One-Time Donations: www.paypal.com/donate?hosted_...
    💰 Patreon: / techwithtim
    ◼️◼️◼️◼️◼️◼️◼️◼️◼️◼️◼️◼️◼️◼️
    ⭐️ Tags ⭐️
    - C++ Tutorial
    - C++
    - Coding Tutorial
    - Tech With Tim
    - Sets
    ⭐️ Hashtags ⭐️
    #TechWithTim #C++Programming

ความคิดเห็น • 42

  • @steeric2053
    @steeric2053 3 ปีที่แล้ว +58

    0:27 That is a major misstatement. In C++ `std::set`s are *ordered*, and accesses don't have a constant time complexity. For unordered sets, use `std::unordered_set`, which uses hashing to implement constant time accesses.

    • @TechWithTim
      @TechWithTim  3 ปีที่แล้ว +17

      Thank you for pointing that out! My mistake on that, I’m used to Python sets.

    • @fenio4689
      @fenio4689 2 ปีที่แล้ว +12

      @@TechWithTim please edit the video that caused me some trouble

    • @KajalSingh-if4uu
      @KajalSingh-if4uu 2 ปีที่แล้ว

      Thanks. I was confused that what is the difference between set and unordered_set.

  • @anakonda1709
    @anakonda1709 ปีที่แล้ว +1

    Thank you TIm with your tutorials a passed the olympiad in Kazakhstan

  • @Dzonex_
    @Dzonex_ 22 วันที่ผ่านมา +1

    5:10 There is a bug that can happen when the element 'C' is located at the end of the set. The find() func returns a iterator pointing to the value that is located, end() func returns a iterator to the end of the set. However if the 'C' value is at the end and we say if it's equal to the end of the set then it will say that 'C' was not found. The correct code should be
    if(s1.find('C') != s1.end()){
    cout

  • @dealvin
    @dealvin 3 ปีที่แล้ว +3

    Thank you for this videos. I hope you that do something project with c++ , basics and complex.

  • @ahmadb.e.k4629
    @ahmadb.e.k4629 4 หลายเดือนก่อน

    Thanks bro that is nice elaboration !

  • @piyanshigupta2631
    @piyanshigupta2631 3 ปีที่แล้ว

    Hlo sir
    Your video are really very supportive. You are doing a appreciable work by sharing your knowledge and experience with us.
    Thanx alot

  • @dealvin
    @dealvin 3 ปีที่แล้ว +2

    More c++ videos, please.

  • @zauq786
    @zauq786 ปีที่แล้ว

    Nyc Work!

  • @dilshadnajimi6955
    @dilshadnajimi6955 3 ปีที่แล้ว +4

    Hello Tim do u have any plan to teach gui in the advance programming?

  • @yourdadsbestfriend7101
    @yourdadsbestfriend7101 3 ปีที่แล้ว

    thanks tim

  • @ethanshay7819
    @ethanshay7819 3 ปีที่แล้ว

    Thanks for the tutorials. It would be very helpful to beginner and intermediate projects to help with learning.

  • @barrotem5627
    @barrotem5627 3 ปีที่แล้ว +4

    You're fluid and eloquent, well done ! Keep up the great work. 😃
    Edit : I hope fluid is an appropriate word here... I meant he is continuous, you know?
    (Please correct me 😆)

    • @TechWithTim
      @TechWithTim  3 ปีที่แล้ว +2

      Thank you! Cheers!

  • @MusicalGeniusBar
    @MusicalGeniusBar 3 ปีที่แล้ว

    Can sets also work for classes eg if this class(person) is in this set ?

  • @KyranFindlater
    @KyranFindlater 3 ปีที่แล้ว

    can you explain in further detail why you prefer ++i instead of i++ for a loop incrementor statement? In the scope of a simple for-loop, that incrementor/end of loop statement happens at the end of the loop and due to the order it doesn't matter if you post-increment or pre-increment the variable.

    • @AlFredo-sx2yy
      @AlFredo-sx2yy 2 ปีที่แล้ว +5

      since he doesnt explain it, i guess i will help you out ...
      ++i just increments the value of i and returns the new value of i
      i++ has to store the value of i in a temporary variable, increment the value of i the same way ++i does and then return the temporary.
      Both of these operations are really cheap, but back in the day, every single cycle of processing power really mattered, so the small overhead i++ might have over ++i really mattered back then.
      Nowadays, there's 2 reasons as for why using i++ doesnt matter that much: because computers are more powerful and that barely affects anything with regular ints, and because modern compilers automatically optimize all of your i++'s in loops with ++i's when there is no difference between the 2, so there's no performance impact.
      The reason ++i is still prefered nowadays is because this operator can be overloaded for different classes or structs, for example, iterators. An iterator for a simple vector class for example is simply incrementing by 1 the address of a pointer so its really cheap either way and you wont notice a performance impact when looping, but there might be a class that is really complex and the ++ operator could be doing a lot more work behind the scenes. Most iterators dont really matter, but usually, the ++n implementation is slightly faster than the n++ one because it doesnt have to copy a temporary value.
      The general rule of thump is that ++i is either faster or as fast as i++ when working with custom classes or iterators for custom classes.
      A real life example of why this could matter:
      Imagine i have an "entity" class where i store a lot of information about each entity, such as a string for the name, an unsigned int for its age, some doubles for its X, Y and Z position, etc etc... If i want to store these "entities" inside of a simple list class (for example a linked list or a vector), each element im storing is of a certain custom class that occupies a lot of space in memory per member, because it has to store a lot of member variables for each entity. If i have an iterator, the ++itr will simply return a reference to the next element in the list, while using the itr++ will have to copy the element into a temporary value, because returning references to temporary values is not going to work (the temporary is temporary after all, so you really have to make a copy, you cant return a reference to that) which means im copying the whole element for each loop the iterator does if i use an itr++. For a list of ints this might not matter, but as i said, if im using a class that contains a big amount of data per member, it will slow down the program's performance.
      Here's a simple implementation of the operators n++ and ++n for an iterator for a vector class so you see what i mean:
      //this is the operator that goes ++i
      const T & operator++()
      {
      return ++ptr;
      }
      //this is the operator that goes i++
      T operator++(int)
      {
      T * temp = ptr;
      ++ptr;
      return temp;
      }
      as you can see, the operator for i++ has a lot more info inside.
      Now, putting all of this aside, let me remind you this is for custom classes. For regular ints in a for loop it doesnt really matter because as i already said, modern compilers will automatically optimize that so you can use i++ if you want if you are working with ints. If you are working with anything else, just to be safe use ++i for performance.
      Im not english so sorry if the explanation was not clear enough. I did my best i guess :P

  • @techwithdipufrom0ton621
    @techwithdipufrom0ton621 8 หลายเดือนก่อน

    I want to learn some c++ projects with you Tim. How about building some apps and games?

    • @lucabricot_78
      @lucabricot_78 8 หลายเดือนก่อน

      yes siiiiiir

  • @Jfti9200
    @Jfti9200 2 ปีที่แล้ว

    Help me with my survey.❤
    What do you guys personally want the features of your device but it is not available yet? Or what you want to bring change in your device?
    I hope this is understandable🙏

  • @Jordan4Ibanez
    @Jordan4Ibanez 2 ปีที่แล้ว +1

    I think I over engineered my solution a bit
    void MyOutPutTest(){
    std::string test = "This is a test iii jj j hgahs aaaaaaaa";
    std::string find = "hello";
    std::set initialLetters;
    std::set findLetters;
    for (char const &letter : test){
    initialLetters.insert(letter);
    }
    for (char const &letter : find){
    findLetters.insert(letter);
    }
    bool allLettersInTest = true;
    for (char const &letter : findLetters){
    bool found = false;
    for (char const &secondaryletter : initialLetters){
    if (!found && letter == secondaryletter){
    found = true;
    continue;
    }
    }
    if (!found){
    allLettersInTest = false;
    break;
    }
    }
    if (allLettersInTest){
    std::cout

  • @mba2ceo
    @mba2ceo ปีที่แล้ว

    still has to check :) under the hood

  • @tambaserchamling6375
    @tambaserchamling6375 3 ปีที่แล้ว +3

    Hi I am from nepal
    Someone said me c++ is not good programming language but I want to learn it and can I get job in software developer after learn c++
    I want to become software developer

    • @zombiekiller7101
      @zombiekiller7101 3 ปีที่แล้ว +1

      You need to learn atleast 2-3 programming languages.
      Each programming language is good in a specific task.

    • @gagan-tb2pp
      @gagan-tb2pp 3 ปีที่แล้ว +2

      It is a good language don't let anyone decide what you will do in the future. If you like it do it if not then it's up to you. I learned 3 languages even tho I only needed one. The more you learn the better your brain will be trained. good luck

    • @tambaserchamling6375
      @tambaserchamling6375 3 ปีที่แล้ว

      @@zombiekiller7101 thank you so much sir

    • @tambaserchamling6375
      @tambaserchamling6375 3 ปีที่แล้ว

      @@gagan-tb2pp thank you so much sir

  • @dinakarandavid8936
    @dinakarandavid8936 3 ปีที่แล้ว +1

    Hllo bro

  • @dr.merlot1532
    @dr.merlot1532 2 ปีที่แล้ว

    The Black Gulch.

  • @Pablex3D
    @Pablex3D ปีที่แล้ว +1

    You just cut down a 2 hour lesson to a few minutes for me, thanks

  • @oskarpravst3686
    @oskarpravst3686 3 ปีที่แล้ว

    hey

  • @pv9060
    @pv9060 3 ปีที่แล้ว

    Hi

  • @johnchu869
    @johnchu869 ปีที่แล้ว

    mark

  • @nuswe5778
    @nuswe5778 3 ปีที่แล้ว +1

    Helloooo

  • @RekhaRani-mu9od
    @RekhaRani-mu9od 3 ปีที่แล้ว

    Hiiiiiii

  • @fahmidurarnob2470
    @fahmidurarnob2470 3 ปีที่แล้ว

    You should start using '
    ' instead of endl
    😶😶

    • @KyranFindlater
      @KyranFindlater 3 ปีที่แล้ว

      why?
      is only a new line on linux systems, you need
      for windows. using std::cout and std::endl it allows you to write code in a platform-independant way, and depending if you compile for linux or windows or otherwise aren't sure of the target platform, letting the C Std libraries handle this for you is significantly better practice.

    • @fahmidurarnob2470
      @fahmidurarnob2470 3 ปีที่แล้ว

      ​@@KyranFindlater std::endl is an object that append the new line character and flush stdout buffer for this reason it takes more time to process. but '
      ' is a string of length 1 that gets append to stdout so it is faster than std::endl.

    • @FaranAiki
      @FaranAiki ปีที่แล้ว +1

      @@KyranFindlater
      Now, now, have you found the reason why you are wrong?

  • @c4g3dB34st
    @c4g3dB34st 3 ปีที่แล้ว

    First