2016-03-04 3 views
-2

Я пытаюсь реализовать вектор связанных списков, но я сталкиваюсь с проблемой при реализации функции insert.C++ HashTable Вставить функцию

Определение класса: реализация

#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); 
+0

Что такое 'a' здесь, которое вставлено push_back? –

+1

Не 'новый вектор'. Для этого не существует никакой причины. Сделайте вектор членом класса напрямую. –

+0

включите определение вашего класса, пожалуйста. – kfsone

ответ

1
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, сделайте вектор переменной-членом класса. При необходимости вы можете очистить соответствующую память в деструкторе класса Hash Table.

Смежные вопросы