-2

I know this may not just be for hash tables but an answer with respect to it will help me better understand it:

int hash (const string & key, int tableSize) { int hashVal = 0; for (char ch : key) // ????lost here??? is ch is just any character in the key??? hashVal += ch; return hashVal % tableSize; } 
8
  • What language is this? You've not included a programming language tag. You've just posted a single line of out-of-context code. Commented Apr 16, 2021 at 1:08
  • Sorry the language is C++ Commented Apr 16, 2021 at 3:39
  • Please post code no images Commented Apr 16, 2021 at 3:41
  • 1
    en.cppreference.com/w/cpp/language/range-for Commented Apr 16, 2021 at 3:44
  • 1
    Your tag edit was good. The rest of it made things worse. Your experiences here will be much better if you spend some time taking the tour and reading the help center pages to learn how the site works before posting. You should also read Please do not upload images of code/errors when asking a question.. Commented Apr 16, 2021 at 3:44

1 Answer 1

1

A string is considered to be a collection of characters.

Explanation: For each character in the string key, perform the body of the loop.

ch : key

  • ch is the name of a loop variable, it will be assigned one character at a time from the string called key, and the loop body will be executed with that value of ch iteratively
  • : this delimits the loop variable from the string.

See "Range-based for loop" (since C++11)

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.