c++stdlist

Declaring a std::list with an array index C++


I was following a hash table implementation online (https://www.youtube.com/watch?v=2_3fR-k-LzI) when I observed the video author initialize a std::list with an array index. This was very confusing to me as I was always under the impression that std::list was always meant to operate like a linked list and was not capable of supporting random indexing. However, I thought it was maybe a weird way to declare the size of a list and ignored it and moved on. Specifically, he did the following:

static const int hashGroups = 10;
std::list<std::pair<int, std::string>> table[hashGroups];

Upon trying to implement a function to search to see if a key resided in the hash table, I realized that I could not access the std::list objects as I would expect to be able to. In HashTable.cpp (which includes the header file that defines the two variables above) I was only able to access the table member variable's elements as a pointer with -> instead of with . as I would expect to be able to. It looks like what is directly causing this is using the array index in the list definition. This seems to change the type of the table variable from a std::list to a pointer to a std::list. I do not understand why this is the case. This also appears to break my current implementation of attempting to iterate through the table variable because when I declare an iterator to iterate through table's elements, I am able to see that the table has the correct data in the VS debugger but the iterator seems to have completely invalid data and does not iterate through the loop even once despite seeing table correctly have 10 elements. My attempt at the search function is pasted below:

std::string HashTable::searchTable(int key) {
   for (std::list<std::pair<int, std::string>>::const_iterator it = table->begin(); it != table->end(); it++)
   {
      if (key == it->first) {
         return it->second;
      }
      std::cout << "One iteration performed." << std::endl;
   }
   return "No value found for that key.";
}

With all of this being said, I have several burning questions:

  1. Why are we even able to declare a list with brackets when a std::list does not support random access?
  2. Why does declaring a list like this change the type of the list from std::list to a pointer?
  3. What would be the correct way to iterate through table in its current implementation with an iterator?

Thank you for any help or insight provided!


Solution

  • After reading the responses from @IgorTandetnik I realized that I was thinking about the list incorrectly. What I didn't fully understand was that we were declaring an array of lists and not attempting to initialize a list like an array. Once I realized this, I was able to access the elements correctly since I was not trying to iterate through an array with an iterator for a list. My revised searchTable function which to my knowledge now works correctly looks like this:

    std::string HashTable::searchTable(int key) {
       int hashedKey = hashFunction(key);
    
          if (table[hashedKey].size() > 0)
          {
             for (std::list<std::pair<int, std::string>>::const_iterator it = table[hashedKey].begin(); it != table[hashedKey].end(); it++)
             {
                if (key == it->first) {
                   return it->second;
                }
             }
       }
       return "No value found for that key.";
    }
    

    And to answer my three previous questions...