How can I print all the values in this linked list inside a hash table?

440 Views Asked by At

I am not exactly able to code how do I print all the values that will be there in this linkedlist at a particular hash value?

    unordered_map<string,list<string>> myhash;
    unordered_map<string,list<string>>::iterator it;
    for ( i = 0; i < N; i++ )
    {
        string foo = arr[i];
        sort(foo.begin(),foo.end());
        myhash[foo] = list.insert(arr[i]); // Is this the correct way of inserting elements in the linked list?
    }
    for ( i = 0; i < N; i++ )
    {
        string foo = arr[i];
        sort(foo.begin(),foo.end());
        it = myhash.find(foo);
        if ( it!= myhash.end() )
        {
            //if the key value is found, I want to print all elements in the linked list present at that key value.
           // also, after printing all the elements, I want to delete that key from the hash table.
        } 

    }

So, I have doubts basically in the parts where I have added comments?

2

There are 2 best solutions below

4
On BEST ANSWER

im quite confused what you are doing oO

Is that what you are searching for?

std::unorderd_map<std::string, std::list<std::string>> hash_map;

// fill map
....

// go to hash
std::string hash = "whatever";
std::unorderd_map<std::string, std::list<std::string>>::iterator itr;
itr = hash_map.find(hash);

// check if value exists
if(itr == hash_map.end())
   std::cout << "not in map ... " << std::endl;
else {
   // print everything
   const std::list<std::string> & hash_list = (*itr).second;
   for(const std::string & value : hash_list)
      std::cout << value << std::endl;

   // sry edit of course the delete
   hash_map.erase(itr);
}
0
On

I am trying to figure out what you are trying to do but the code you provided isn't clear enough.

First of all your code shouldn't compile; on line 7, where I assume you are trying to insert elements into the list at myhash[foo], you are trying to access the list through the "list" class and you are also using the assignment operator in a way that obviously won't work. If what I assumed is correct then, what you should actually do is insert the arr[i] directly into the specific list that is returned by the [] operator (the list at the foo key) in this way,

myhash[foo].insert(arr[i]);

Also, on line 13, you are calling the find function with a string parameter which again obviously won't work because your unordered_map is made up of lists not strings, the strings are keys in your case and if you want to find the list at a certain key you can do it right away : myhash[foo]. If you are trying to find a specific string in the list at myhash[foo], then do it this way

it = myhash[foo].find(foo);

and in this case, your operator should be defined in this way,

list<string>::iterator it;

About the last comment you put, to print the list, all you have to do is deference the iterator you have using the * operator to get the list and then print it.

    for(list<string>::iterator it_list = (*it).begin(); it_list!= (*it).end(); it_list++) 
cout<<*it_list;

and to delete the list: myhash.erase(it);

Finally, I guess that you don't have a solid understanding of hash tables and unordered maps (which aren't exactly the same thing by the way) and I think that you should read more on this topic.