带有向量的哈希表,检索大小时出错

问题描述

我正在向函数添加一些代码,最后我有一个用于单独链接的哈希表。我试图检索散列中所有未删除项的散列表大小,即函数CONTAINER ID,IMAGE,COMMAND,CREATED,STATUS,PORTS,NAMES 8c0e092b6815,thomsch98/kafdrop:latest,"/usr/local/bin/mvn-…",3 days ago,Up 3 days,PAServices_kafdrop.1,yen4hgju18kkfgq9bvud7e1w8 这是我认为相关的代码

int size()

我不断收到错误消息:

 #include <iostream>
#include <vector>
#include <list>
#include <stdexcept>

// Custom project includes
#include "Hash.h"

// Namespaces to include
using std::vector;
using std::list;
using std::pair;

//
// Separate chaining based hash table - inherits from Hash
//
template<typename K,typename V>
class ChainingHash : public Hash<K,V> {

    int table_size;

private:
    vector<list<V>> table;          // Vector of Linked lists

public:

    ChainingHash(int n = 11) : table(n){
    
    }

    ~ChainingHash() {
        //this->clear();
    }

    //Test for empty hash
    bool empty() {
        
        if (!table.empty()) {
            return false;
        }
        
        return true;
  
    }

    //Quantity of (non-deleted) elements in hash
    int size() {
   
        int count = 0;
    
        for (int i = 0; i < table.size(); i++) {
            if (table[i] != nullptr) {
                count++;
            }
        }
        
        return count;
    
    }
    

有人可以帮我解决这个问题吗?

解决方法

编译器抱怨 List<T> 没有提供带有 nullptr 的比较运算符,而且它不是一个指针。

您可以使用 table[i].empty() 来知道它是否为空。

您可以使用 erase 删除向量的元素。

,

对于这段代码,您可以使用

int count = 0;    
for(auto i : table) ++count;

这将有助于您的目的