c++ - 这个哈希表中的“:”和“ch”是什么意思循环“for(char ch:key)”?
问题描述
我知道这可能不仅适用于哈希表,而且关于它的答案将帮助我更好地理解它:
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;
}
解决方案
字符串被认为是字符的集合。
解释:对于字符串中的每个字符key
,执行循环体。
ch : key
ch
是一个循环变量的名称,它会从名为 key 的字符串中一次分配一个字符,并且循环体将使用该值ch
迭代地执行:
这将循环变量与字符串分隔开来。
参见“基于范围的 for 循环”(C++11 起)
推荐阅读
- r - 删除两个文本之间的第一位逗号和空格以及最后一个逗号或空格
- python - 堆叠一个 numpy 数组列表
- c++ - Boost property_tree 错误:在 .ini 文件中获取元素时,将数据转换为“j”类型失败
- javascript - 在 Angular 9 中分配表单值
- 2sxc - 2sxc 11 字体图标选择器 Material Icons
- json - 无法快速解析 json
- python - 如何使用 python 读取控制台响应?
- laravel - Laravel 7 - 违反完整性约束:1048 列“名称”不能为空
- firebase - Firebase 分析受众编辑/删除已禁用
- react-native - 使用 axios.get 时,componentDidMount 给出“请求失败,状态码为 401”