【问题标题】:Returning empty iterator in C++ hash table code在 C++ 哈希表代码中返回空迭代器
【发布时间】:2015-10-24 19:17:56
【问题描述】:

我正在关注一些关于使用单独链接实现哈希表的手写笔记。特别是我正在寻找这个功能:

list<ListCell>::iterator TablaHash::FoundOnList(int key, int table_row){
    for(list<ListCell>::iterator found= table[table_row].begin(); 
            found != table[table_row].end();found++){
        if((*found).Key() == key){
            return found;
        }
    }
    return Ø;
}

最后一个符号是空集。我知道我应该在那里返回一个“空”迭代器,但我该怎么办?也许返回 end() 迭代器?

【问题讨论】:

    标签: c++ hash iterator hashtable isnullorempty


    【解决方案1】:

    c++ 中没有空迭代器的概念。您应该返回 end() 迭代器。甚至标准的 STL 算法也是如此。

    【讨论】:

      【解决方案2】:

      是的,返回 end() 迭代器是 C++ 中最自然的事情。

      例如,std::find 如果找不到所搜索的项目,则返回 end 迭代器。

      【讨论】:

      • 我可以用 0 代替吗?之后有一个函数返回 FoundOnList(key,table_row) != 0 来查看表上是否存在某个元素。
      猜你喜欢
      • 2013-06-21
      • 2023-04-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-12-25
      相关资源
      最近更新 更多