C++哈希表插入功能
C++ HashTable Insert Function
我正在尝试实现链表向量,但是在实现插入函数时遇到了问题。
类定义:
#include <vector>
#include <list>
#include <string>
using namespace std;
class HashTable {
public:
HashTable(int size);
unsigned int hash(string key);
void insert(string x);
private:
vector<list<string> >* table;
类实现:
#include "hashTable.h"
#include <string>
#include <list>
#include <vector>
using namespace std;
HashTable::HashTable(int size) {
table = new vector<list<string> >(size);
}
HashTable::insert(string x){
unsigned int index = hash(x); //left out the hash function for brevity
table[index].push_back(x);
}
我收到错误:
hashTable.cpp:38:26: error: no viable conversion from 'string' (aka
'basic_string<char, char_traits<char>, allocator<char> >') to
'const value_type' (aka 'const
std::__1::list<std::__1::basic_string<char>,
std::__1::allocator<std::__1::basic_string<char> > >')
table[index].push_back(x);
vector<list<string>>* pTable = new vector<list<string>>(100);
unsigned int index = 3;//Assume that hash("AAA")=3
(*pTable)[index].push_back(string("AAA"));
上面的代码有效。可能您没有将正确的字符串对象传递到列表对象中。正如@Mooing Duck所说,使向量成为类的成员变量。如果需要,可以在哈希表类的析构函数中执行相应的内存清理。
相关文章:
- 如何编写用于多映射插入和擦除功能的 API?
- 如何将标准::矢量插入具有自定义排序功能的 std::set 中
- C++插入排序错误功能不起作用
- 插入运算符的过载与使用二传手功能相比
- 使用链表.为什么我的插入功能不起作用?
- 二叉树插入功能不适用于 10 个条目
- 插入功能保持重新创建根节点
- 如何使用字符串更改此插入功能以在链接列表中工作
- 双链接列表插入功能C
- LLVM插入功能调用到另一个函数中
- 创建通用插入功能
- 有什么方法可以将此功能分开并插入空间
- 自定义char*插入功能多次运行时会产生运行时错误
- 哈希表打印和插入功能
- 二叉搜索树在插入功能方面有问题
- C++中的插入器和提取器功能
- 插入功能指向新数据
- QVECTOR插入功能后无法访问点
- GCC vs Clang:将功能与-FPIC一起插入
- 插入功能模板专业化