【问题标题】:How to create a constructor using a hash table with Vectors (separate Chaining)如何使用带有向量的哈希表创建构造函数(单独的链接)
【发布时间】:2021-06-25 02:54:12
【问题描述】:

我正在尝试为单独链接的向量哈希表创建一个构造函数。我不断收到一条错误消息:

错误:“表”之前的预期主表达式

#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> {
private:
    vector<list<V>> table;          // Vector of Linked lists

public:
    ChainingHash(int n = 11) {

        table = vector<list<K,V>> table(n);

    }

【问题讨论】:

    标签: c++ vector constructor hashtable


    【解决方案1】:

    这条线没有意义。您正在尝试将赋值表达式与变量定义语句结合起来。不仅如此,当您的存储表需要list&lt;V&gt; 时,您还有list&lt;K,V&gt; 这毫无意义...

    table = vector<list<K,V>> table(n);  // <-- completely broken syntax
    

    要在构造函数中初始化你的表,你需要做的就是使用一个初始化列表:

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

    使用上面的构造函数定义,table 成员将被初始化为 n 空列表,这似乎是您想要做的。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-02-27
      • 1970-01-01
      • 2017-11-03
      • 2022-01-10
      • 2021-02-01
      • 1970-01-01
      • 2019-11-26
      • 2016-07-14
      相关资源
      最近更新 更多