-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;
}
Casey
  • 10,297
  • 11
  • 59
  • 88
  • What language is this? You've not included a programming language tag. You've just posted a single line of out-of-context code. – Ken White Apr 16 '21 at 01:08
  • Sorry the language is C++ – Tricepticon Apr 16 '21 at 03:39
  • Please post code no images – Mike Slinn Apr 16 '21 at 03:41
  • That is a range based for loop in C++. – Mutating Algorithm Apr 16 '21 at 03:43
  • 1
    https://en.cppreference.com/w/cpp/language/range-for – Hans Passant Apr 16 '21 at 03: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] 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.](//meta.stackoverflow.com/q/285551). – Ken White Apr 16 '21 at 03:44
  • @KenWhite sorry I am trying to get the hang of using this website properly. I will take the tour and read the help center pages. – Tricepticon Apr 16 '21 at 04:08
  • duplicates: ['colon' and 'auto' in for loop c++? need some help understanding the syntax](https://stackoverflow.com/q/35490236/995714), [C++ weird for loop syntax](https://stackoverflow.com/q/42657234/995714), [What is “for (x : y)”?](https://stackoverflow.com/q/24946027/995714) – phuclv Apr 16 '21 at 04:13

1 Answers1

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)

Mike Slinn
  • 7,705
  • 5
  • 51
  • 85