C++ Hash Table Implementation

Поділитися
Вставка
  • Опубліковано 12 гру 2024

КОМЕНТАРІ • 183

  • @Mmmmmkoogfssdvbhvggg
    @Mmmmmkoogfssdvbhvggg 4 роки тому +57

    Man! You are just amazing! I have coding interviews in a few days, and you just saved my ass. You have implemented almost all DS, and they are so neat and clean, the way it should be! Thanks man!

  • @soniasingh532
    @soniasingh532 3 роки тому +3

    love the perfection. the way you type and your explanation

  • @strawberrysultan
    @strawberrysultan 8 місяців тому +3

    Thank you!! This was so helpful. Supplementing my classes with your videos now - my teacher only reads from the book.

  • @luceracox7686
    @luceracox7686 5 років тому +6

    This was incredibly helpful. You explain each step clearly which is hard to find! Thank you:)

  • @nadiequintero9981
    @nadiequintero9981 5 років тому +12

    Very useful and clear video, thank you for helping me understand hash tables better.

  • @MrJQ3d
    @MrJQ3d 2 роки тому +4

    super amazing work man, props! you're a great programmer and teacher.

  • @chikin8895
    @chikin8895 3 роки тому +56

    This is actually ASMR lol

  • @panicineurope
    @panicineurope 4 роки тому +2

    Thanks guy! Very nice tutorial. Here's my go at the search function:
    string HashTable::searchTable(int key) {
    int hashValue = hashFunction(key);
    auto& cell = table[hashValue];
    auto bItr = begin(cell);
    bool keyExists = false;
    for (; bItr != end(cell); bItr++) {
    if (bItr->first == key) {
    keyExists = true;
    return bItr->second;
    }
    }
    if (!keyExists)
    return "Result Not Found";
    }

    • @ilanogaming5034
      @ilanogaming5034 2 роки тому

      Hi there, how do i search for the other same key?
      for example :
      HT.insertTable(99, "Jim");
      HT.insertTable(99, "Tim");
      how do i search for value "Tim"? cause if i search for key 99 it displays "Jim"

    • @erlisibraimi5267
      @erlisibraimi5267 2 роки тому

      @@ilanogaming5034 you can't have 2 different values with the same key, that's why your value gets overridden

  • @nnewram9936
    @nnewram9936 4 роки тому +2

    in isEmpty, you dont need a sum. Just return true whenever the size is non zero in the loop, it will on average save a lot of execution time

    • @CodingJesus
      @CodingJesus  4 роки тому +1

      Yeah, others have brought it up below. That's a good approach.

    • @nnewram9936
      @nnewram9936 4 роки тому

      @@CodingJesus oof, didn't catch that. Well, great video anyways! Thanks for it.

  • @jasmeetsingh7499
    @jasmeetsingh7499 4 роки тому +9

    isEmpty can be implemented in a better way (O(1) as opposed to O(hashGroups)), You could create a variable that stores number of empty hashGroups. So, numEmptyHashGroups = hashGroups initially. On insert, decrement numEmptyHashGroups if it is the first node in the list. Similarly when removing the item, increment numEmptyHashGroups if it is the last value. Implementation of isEmpty will become as simple as numEmptyHashGroups == hashGrorups. Also, in current implementation you don't need to calculate the sum, you could just return false if at any point the list is not empty. Aggregating the sum can cause overflow.

    • @CodingJesus
      @CodingJesus  4 роки тому +3

      You can keep count of the amount of items successfully added and removed. Call isEmpty() will check whether itemCount is 0 or not.

  • @rohitk2629
    @rohitk2629 4 роки тому +23

    The keyboard sounds like asmr. relaxing..

  • @halily.2626
    @halily.2626 4 роки тому +7

    thanks a lot. But I think using C sytle array is bad practice. The books always say use vector:
    I mean instead of this: list table[hashGroups];
    isn't this better ? : using listType = list;
    vector table;
    Maybe, I am wrong, what do you think ?

    • @grapesalt
      @grapesalt 3 роки тому +1

      std::vector will be slower than C style array's.

    • @Gabriel-hh1pw
      @Gabriel-hh1pw 18 днів тому +1

      Using vector is less efficient than using stack-allocated arrays as allocating memory in heap is slower. This is just an example, but I guess when making a class you'd indeed use a vector so it's more flexible. Can't recall if stl uses Red-Black tree for unordered_map tho.

  • @franky0226
    @franky0226 4 роки тому +6

    That was a neat explanantion!
    thanks a lot, man!

  • @miclaymon
    @miclaymon 4 роки тому

    thanks for helping me write my first working hash function. test on this tomorrow in 12 hours...

  • @adnanlokhandwala9002
    @adnanlokhandwala9002 4 роки тому +2

    Thank you, this was quite helpful, and quite catchy too XD. Keep up the good work, and all the very best

  • @alphabeta3014
    @alphabeta3014 4 роки тому +2

    amazing lecture man. crystal clear.

    • @CodingJesus
      @CodingJesus  4 роки тому +1

      Thanks, please consider subscribing for more.

  • @grayscale-bitmask
    @grayscale-bitmask 3 роки тому

    Great video! Especially in covering effective collision handling, since not all tutorials do that very well. FWIW, 1:25 should be "what it looks like" or just "how it looks". "Like" sounds a bit redundant if you pair it with "How". One of the best tutorials on this topic in any case.

  • @nathansantos7421
    @nathansantos7421 Рік тому

    Muito obrigado Jesus Cristo dos códigos, você é o cara, a main.

  • @khaledelgharbi5200
    @khaledelgharbi5200 3 роки тому

    Great tutorial, this will help me for my interview

    • @khaledelgharbi5200
      @khaledelgharbi5200 3 роки тому

      One question: can we use this hash class in std::unordered_set ?

  • @ElemenTalParkour
    @ElemenTalParkour 2 роки тому

    Great video, thanks!!
    You would not have to go over all the table to check if is empty, if you found one element - return false.

  • @ibrahimvelasquez3874
    @ibrahimvelasquez3874 Рік тому

    What keyboard are you using? It sounds good!

  • @brycejohansen7114
    @brycejohansen7114 3 роки тому +2

    Why do you put return at the end of functions that have void as their return value?

  • @divyanshr1
    @divyanshr1 3 роки тому +4

    Thanks man, I have searched google, youtube, your video is the only source of hashmap implementation in c++. But one thing is that as we are using for loops in this code, it doesnot mean O(1) operations, right?

    • @getsuha
      @getsuha 3 роки тому +2

      for loop gets used when there is collision and the worst case time complexity for collision is actually O(n), i.e. when all the values have collided. It would be O(logn) if implemented with an AVL tree.

  • @marflage
    @marflage 4 роки тому +3

    Just what I was looking for. Amazing explanation. However, I can see that some beginners can get confused because of list, iterators and i{}. Nonetheless, it was a great video.
    PS: That keypresses and mouse clicks were hypnotic.

    • @CodingJesus
      @CodingJesus  4 роки тому +1

      I was actually afraid that people would not like the typing sound and it would annoy them. But people seem to love it. I have another video coming out on Wednesday at 5:30 CST with more typing sounds. :D

    • @austintaylor5372
      @austintaylor5372 4 роки тому +1

      Yeah could you explain the i{} and how he did the iterator. I'm also trying to learn c++ i only code in c#

    • @CodingJesus
      @CodingJesus  4 роки тому +4

      @@austintaylor5372 int x declares x but doesn't initialise it. By that I mean that 'x' has junk in it, and by junk I mean it can have anything in it. int x{} default-initialises x to int's default value, that being 0.
      Iterators are like pointers in a sense, except they are used on collections and there are several types of them. They allow you to iterate over a collection and, in some cases (non-const iterators), modify the contents of that collection. Hope this helped.

    • @austintaylor5372
      @austintaylor5372 4 роки тому

      @@CodingJesus yeah that makes alot of sense thank you :)

  • @jakobhuetter
    @jakobhuetter 2 роки тому

    Very helpful! I do have one question tho: you are saying in the beginning, that this would be hashing with seperate chaining. But wouldnt you append the value to the list of the other ones of the same hash value instead of replacing them in seperate chaining? Line 46 in your code. Or am i missing something there?

  • @ayaz.unstoppable
    @ayaz.unstoppable 3 роки тому

    Thank u this question was asked in sprinklr interview

  • @sharinganuser1539
    @sharinganuser1539 4 роки тому +1

    didn't understood why list::erase() can lead to segfault....even if last item is erased it will simply return container end ....how getting next itr position can lead to error?

  • @user-qh9xl9ry7d
    @user-qh9xl9ry7d Рік тому +1

    Umm... How do you overcome Hash Collision?
    I can't find it. How to overcome Hash Collision in this code

  • @maishananjeeba7436
    @maishananjeeba7436 Рік тому

    is this code also considers the collison problem?

  • @aryanparekh9314
    @aryanparekh9314 4 роки тому +2

    I really love this. One thing I am not able to understand is why the code will not work if you don't put a "&" operator in front of cell = hashFunction(key). Can anybody explain it to me?

    • @CodingJesus
      @CodingJesus  4 роки тому

      What do you mean? Paste the code into Godbolt and share the generated link with me

    • @aryanparekh9314
      @aryanparekh9314 4 роки тому

      @@CodingJesus I can't believe you replied!! I didn't know the concept of references in c++ so I didn't understand why you used a & operator in front of a variable. Well I understood it now but I do have a few doubts. Is this method faster than using a plain vectors? What is the best scenario in which I should use this?

    • @CodingJesus
      @CodingJesus  4 роки тому

      @@aryanparekh9314 I still don't know what part of the video you're talking about.

    • @aryanparekh9314
      @aryanparekh9314 4 роки тому

      @@CodingJesus Okay I'm sorry for being unclear, here is the time stamp 6:52 line:40
      When you used cell as a reference

    • @CodingJesus
      @CodingJesus  4 роки тому

      @@aryanparekh9314 You should use references when you want to change the value stored in the hash-table. Not aliasing the hash-table element with a reference will lead to you crating a copy of it. When you go to assign cell = something, it won't be stored in the hashtable, because 'cell' isn't a reference.

  • @pranavbelgaonkar8634
    @pranavbelgaonkar8634 3 роки тому +1

    Hi ,
    I learned a lot from this video but i did not understand why you defined variable and then placed a curly bracket after it. Why not just leave it blank. thanks!

    • @CodingJesus
      @CodingJesus  3 роки тому

      Google the difference between defining a variable and initializing it.

  • @zombiehellmonkeygaming1956
    @zombiehellmonkeygaming1956 4 роки тому

    Thanks so much for this, it helped a lot!

    • @CodingJesus
      @CodingJesus  4 роки тому

      Glad it helped! Please consider subscribing for more help :D

  • @kantocoder5046
    @kantocoder5046 3 роки тому +1

    Why are you using {} instead of = in the variables initialization? To make your code harder to comprehend?

    •  3 роки тому +1

      better convention for OOP , it's recommended to use it this way by the standard because it reduce time complexity by avoiding useless operation

    • @kantocoder5046
      @kantocoder5046 3 роки тому +2

      @
      >> recommended to use it this way by the standard >because it reduce time complexity by avoiding useless operation

    • @brainyfool4846
      @brainyfool4846 3 роки тому +1

      @ please please tell more or put a link when to use variable{} for initialization or @Coding Jesus please explain.

  • @greglouis5779
    @greglouis5779 3 роки тому +1

    i have a c++ project of storing 500 books from a .txt file in which there are details about the author,ISBN ,title, etc. and I have to find a convenient data structure for that to do insertion, deletion,searching can you help me out plz?

  • @prabhjotsingh3172
    @prabhjotsingh3172 3 роки тому

    Did you not implement SearchTable function?

  •  4 роки тому +1

    Very good explanation!! what IDE is that?

  • @husein_alfil
    @husein_alfil 2 роки тому

    Great Video !!
    Great Job :D

  • @nordgaren2358
    @nordgaren2358 3 роки тому

    Question: Do you actually need the keyExists bool? Couldn't you just return in the for loop if the key exists after replacing it?

  • @processorbot8761
    @processorbot8761 4 роки тому +3

    this is a great video

    • @CodingJesus
      @CodingJesus  4 роки тому

      Thank you! Make sure to check out my other videos.

  • @srihari5605
    @srihari5605 3 роки тому +1

    should have added a key-value pair that gets the same hash!
    excellent work though!

  • @chuyinghe7953
    @chuyinghe7953 3 роки тому +1

    great tutorial! I encountered a problem for using the function emplace_back(), IDE says it "no member named 'emplace_back' in 'std::__1::list

    • @jarreed0
      @jarreed0 3 роки тому +1

      May need to tell your compiler to use c++11

  • @epicjr7260
    @epicjr7260 4 роки тому

    For the private member variable list hashTable, this is just a containter that is in an index of the array right? If so, are we able to use a vector or a linked list instead?

    • @CodingJesus
      @CodingJesus  4 роки тому

      We want to have a set number of groupings, hence we want a container that cannot be resized. An array communicates that intent.

  • @CallBlofD
    @CallBlofD 3 роки тому

    great video man! you explained amazing!!
    (p.s. you forgot const on the print function)

  • @shawn4365
    @shawn4365 3 роки тому

    how would I change this if I wanted each key to hold all of the different names instead of replacing the value

  • @cnp4655
    @cnp4655 3 роки тому

    Since perfect hashing function is assumed, why use the for loops. Doesn't that make the time complexity greater than O(1)?

  • @fridaprince7891
    @fridaprince7891 2 місяці тому

    would you have used templates

  • @japde2694
    @japde2694 2 роки тому

    what keyboard are you using ?!??!?!

  • @makoricodes3262
    @makoricodes3262 3 роки тому

    this guy is doing god's work

  • @santiagomickelleoliverague2337
    @santiagomickelleoliverague2337 2 роки тому

    what is iterator ? im almost at fourth cicle and still without recognise some words

  • @nagarjunvinukonda162
    @nagarjunvinukonda162 4 роки тому

    Hi Jesus, Does Separate changing mean Open addressing? What is the point of checking HT isempty( )or not at start of int main? We are creating Ht from start so of course it will be empty before inserting....

    • @CodingJesus
      @CodingJesus  4 роки тому +1

      Just checking whether IsEmpty is coded correctly. And yes, separate chaining and open addressing are different.

  • @Hoserzzz
    @Hoserzzz 6 місяців тому

    is the video completely corrupted for anyone else?

  • @garrettbellinghausen8389
    @garrettbellinghausen8389 3 роки тому +1

    Did he forget to write the code for the search function?

  • @דורביטון-ר1ר
    @דורביטון-ר1ר 4 роки тому

    in line 44 , dont you need to check if Bitr->first == hashFunction(key) instead of key?

    • @CodingJesus
      @CodingJesus  4 роки тому

      bItr points to key-value pair. bItr->first points to the key. bItr->second points to the value. We are checking whether the key exists, so we need to use bItr->first

  • @happynewyear6123
    @happynewyear6123 3 роки тому

    coding jesus man that name does suit you well

  • @MeriemStudentDjidjeli
    @MeriemStudentDjidjeli 2 роки тому

    awesome video

  • @alexchen6847
    @alexchen6847 2 роки тому

    What's that IDE he's using?
    P.S. Kinda new to C++

    • @jamespolaroid4856
      @jamespolaroid4856 2 роки тому

      thats visual studio code (VSC)

    • @alexvallex5487
      @alexvallex5487 Рік тому

      @@jamespolaroid4856 do you know why my VSC does not like pointers? everytime i use -> it breaks

    • @jamespolaroid4856
      @jamespolaroid4856 Рік тому

      @@alexvallex5487 maybe wrong dash, maybe wrong 'bigger then'-sign - u can use (*variablenameBeforeArrow).variableAfterArrow instead, its just a different representation of the same concept

    • @alexvallex5487
      @alexvallex5487 Рік тому

      @@jamespolaroid4856 thank youu

  • @unknownsahb1670
    @unknownsahb1670 3 роки тому

    display function?? i didnt see it

  • @babaa5701
    @babaa5701 3 роки тому

    nice, video. Wish I had contrast, so I can see () {} [] better.

  • @samiulislamsharan
    @samiulislamsharan 2 роки тому

    how can I implement the searchTable function?

    • @jamespolaroid4856
      @jamespolaroid4856 2 роки тому

      I gave it a try, seems to work, if its the best piece of code - i dont think so.
      string HashTable::searchTable(int key){
      int hashValue = hashFunction(key);
      auto& cell = table[hashValue];
      auto bItr = begin(cell);
      bool keyExists = false;
      string searchValue;
      for (; bItr != end(cell); bItr++) {
      if (bItr->first == key) {
      keyExists = true;
      searchValue = bItr->second;
      cout

  • @hros2671
    @hros2671 3 роки тому +2

    Lmao this guy is called Coding Jesus

  • @flexo666
    @flexo666 4 роки тому +1

    great video! Thank you!

  • @hezhu4851
    @hezhu4851 4 роки тому

    hi, could I know if you miss writing the search function

    • @CodingJesus
      @CodingJesus  4 роки тому

      Yes, I left it as homework for you in particular. But only for you, not for anyone else.

    • @hezhu4851
      @hezhu4851 4 роки тому

      @@CodingJesus And one more thing is here we use separate chaining method. But when we insert, if there's a conflict. You just erase the original value but not adding the value into next node of list. So maybe it is wrong here?

  • @quickyfast-sz9xc
    @quickyfast-sz9xc 9 місяців тому

    bruh I copy line by line and it doesn't run at all.... tells me there's a lot of errors... I don't understand why it doesn't work..

  • @arvinbrian7107
    @arvinbrian7107 3 роки тому

    can anyone tell me which IDE is this?

  • @gourav.barkle
    @gourav.barkle 4 роки тому +1

    can anyone please tell me what is that sum followed by { }.

    • @CodingJesus
      @CodingJesus  4 роки тому +1

      Brace initialization. It sets sum to 0. Please consider subscribing for more content!

  • @carlosniederauer1874
    @carlosniederauer1874 3 роки тому

    Great tutorial!

  • @SuperWhatusername
    @SuperWhatusername 2 роки тому

    Thank you @Coding Jesus

  • @reverse2190
    @reverse2190 2 роки тому

    How do you implement an iterator class for this? anyone got ideas?

  • @SuzyZou1995
    @SuzyZou1995 3 роки тому

    great video!

  • @MidoValo
    @MidoValo 4 роки тому

    amazing explanation thanks a lot
    but tell me why use & in auto& cell = table [hashvalue];
    to give you the address of each hash value or what explain with my all respect

    • @CodingJesus
      @CodingJesus  4 роки тому +2

      We take a reference to that have because we want to alter it, as opposed to altering a copy of it. Hastable[hashKey] returns a reference to the item being held at that index.

  • @Bristol-MyersSquibb
    @Bristol-MyersSquibb 3 роки тому

    Hey u ever get a gpu?

  • @vaibhavgupta8048
    @vaibhavgupta8048 4 роки тому +1

    thanks for the video, but i think in worst case time complexity for searching the value be O(n) in your code which is violating the concept of hash table , if yes ,can it be improved for getting the value in constant time.

  • @mryup6100
    @mryup6100 4 роки тому

    I have the same setup as you! VS code with ubuntu subsytem terminal

  • @telecastersanity
    @telecastersanity 4 роки тому

    no string searchTable(int key) function?

    • @CodingJesus
      @CodingJesus  4 роки тому

      Nope, that's homework for you. ;)

    • @telecastersanity
      @telecastersanity 4 роки тому

      @@CodingJesus lol,
      You sound like me. I just noticed the difference in signatures. Great work and discussion.

    • @CodingJesus
      @CodingJesus  4 роки тому

      @@telecastersanity Subscribe if you want to see more. What would you be interested in? Concurrent queue?

  • @sharuns917
    @sharuns917 3 роки тому

    Nice n quick...thanks

  • @satyagyaparashar2575
    @satyagyaparashar2575 4 роки тому

    So no one is going to talk bout that double intro?

    • @CodingJesus
      @CodingJesus  4 роки тому +1

      Haahaha, I'll crop the intro out. I mentioned in the description to start at 0:16

    • @satyagyaparashar2575
      @satyagyaparashar2575 4 роки тому

      Good content tho..keep it up bro.

  • @pranavnyavanandi9710
    @pranavnyavanandi9710 2 роки тому

    Man, can you make videos like this for java...?

  • @highxavito4875
    @highxavito4875 2 роки тому

    hola buenas tu video me sirvio mucho, pero me gustaria saber como implementar un sistema de busqueda atravez de la key

  • @mockingbird3809
    @mockingbird3809 4 роки тому

    For remove function, Doesn't this work?
    value_at_index.remove_if([&](std::pair capture) -> bool { if(capture.first == key){return true;} else{return false;} });

    • @CodingJesus
      @CodingJesus  4 роки тому

      Why don't you try it out and test for yourself? That's the essence of learning :)

  • @emperorcleon89
    @emperorcleon89 3 роки тому +1

    Thank you Jesus

  • @simple_user000
    @simple_user000 3 роки тому

    Should've zoomed in the screen a little bit......

  • @haiderpall4907
    @haiderpall4907 2 роки тому

    plz attach the source file for helping out

  • @hypermeero4782
    @hypermeero4782 4 роки тому

    where is the search function?

    • @CodingJesus
      @CodingJesus  4 роки тому

      It's homework. If you subscribe I may consider making a data structure with ONLY search ability, nothing else. Not even insert.

  • @focker0000
    @focker0000 3 роки тому

    now next tutorial CPP CON 17: ua-cam.com/video/ncHmEUmJZf4/v-deo.html

  • @mrkunalgoswami2010
    @mrkunalgoswami2010 2 роки тому

    can someone code it in c++ ?

  • @akashmeshram8715
    @akashmeshram8715 Рік тому

    Your teaching is amazing, but the Tabla that you are playing while typing is somewhat irritating.

  • @zackjohnson9387
    @zackjohnson9387 3 роки тому

    I don't understand the purpose of the for loop for the insert function if you're just replacing the value. Why would you need to iterate the cell if you just look at the first value of cell to see if it's the key and if it is replace the second value with the value. Seems unnecessary to me.

    • @zackjohnson9387
      @zackjohnson9387 3 роки тому

      Same with the remove function* Unless you are using chaining what is the point of the for loop?

  • @solsticeprojekt1937
    @solsticeprojekt1937 11 місяців тому

    Audio volume needs to be at least 30% higher, better 40%. It's easy for us to turn the volume lower to acceptable levels, but it's impossible for us to go beyond 100% on both youtube and system volume. Lowering the volume always allows to reach acceptable levels, but the opposite doesn't.

  • @NISHANTRAJ-rl9lf
    @NISHANTRAJ-rl9lf 7 місяців тому

    You Didn't Implemented the SearchTable method😄

  • @TechnologyRules
    @TechnologyRules 4 роки тому

    Is that your real name?

    • @CodingJesus
      @CodingJesus  4 роки тому +6

      Yes, my first name is Coding and my last name is Jesus

  • @umerch8731
    @umerch8731 4 роки тому

    Can we get the source code?

    • @CodingJesus
      @CodingJesus  4 роки тому +1

      No. The point is to watch and learn, not watch and copy-and-paste.

  • @banabana4691
    @banabana4691 4 роки тому +1

    Awesome.

  • @ayubmohammedsjlad4524
    @ayubmohammedsjlad4524 4 роки тому

    hello sir,
    in line 86 the second operand (

    • @felipesilva-mr5om
      @felipesilva-mr5om 4 роки тому +1

      csrc.nist.gov/csrc/media/publications/fips/180/3/archive/2008-10-31/documents/fips180-3_final.pdf

    • @aimilios4794
      @aimilios4794 4 роки тому

      use #include instead of #include

  • @salimrahman2247
    @salimrahman2247 2 роки тому

    Not see clearly plz zoom the screen

  • @jozsefnagy9192
    @jozsefnagy9192 Рік тому

    Amen

  • @naufalmuhadzdzib7338
    @naufalmuhadzdzib7338 3 роки тому

    Big thanks :))

  • @brendankacic6066
    @brendankacic6066 4 роки тому

    Thanks so much!!

  • @sp1xa
    @sp1xa 4 роки тому

    rip Rick

    • @sp1xa
      @sp1xa 4 роки тому

      rip Bob

  • @Anubis10110
    @Anubis10110 4 роки тому

    Amazing

  • @Canyldrm3566
    @Canyldrm3566 3 роки тому

    you like pressing buttons dont you