I want to build a hash table using a linked list
But there is a problem during compilation
I have tried using an array instead of a vector, but still the same result
Maybe it has something to do with the template?
I don't understand the sample code related to template
Here is some code that reproduces the problem, along with the error message
#include <iostream>
#include <vector>
using namespace std;
struct node
{
string key;
string value;
node* next;
};
class HashTable
{
private:
vector<node*> bucket;
long long int capacity;
public:
HashTable(long long int max_size = 100)
{
capacity = max_size;
bucket.reserve(capacity);
bucket.resize(capacity);
fill(bucket.begin(), bucket.end(), NULL);
}
};
Build started...
1>------ Build started: Project: C Test, Configuration: Debug x64 ------
1>C Test.cpp
1>E:\Other\Visual Studio\VC\Tools\MSVC\14.33.31629\include\xutility(4519,24): error C2440: '=': cannot convert from 'const _Ty' to 'node *'
1> with
1> [
1> _Ty=int
1> ]
1>E:\Other\Visual Studio\VC\Tools\MSVC\14.33.31629\include\xutility(4519,24): message : Conversion from integral type to pointer type requires reinterpret_cast, C-style cast or parenthesized function-style cast
1>C:\Users\DaLaw2\source\repos\C Test\C Test.cpp(30): message : see reference to function template instantiation 'void std::fill<std::_Vector_iterator<std::_Vector_val<std::_Simple_types<_Ty>>>,int>(const _FwdIt,const _FwdIt,const int &)' being compiled
1> with
1> [
1> _Ty=node *,
1> _FwdIt=std::_Vector_iterator<std::_Vector_val<std::_Simple_types<node *>>>
1> ]
1>Done building project "C Test.vcxproj" -- FAILED.
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
CodePudding user response:
First, include the proper headers:
#include <algorithm> // fill
#include <cstddef> // size_t
#include <string> // string
#include <vector> // vector
Then don't use NULL
that may be defined as a simple 0
which does not match the node*
you want for std::fill
. Use nullptr
. You also do not need to reserve
just before you resize
.
class HashTable {
private:
std::vector<node*> bucket;
// std::size_t capacity; // use bucket.capacity() instead
public:
HashTable(std::size_t max_size = 100) {
bucket.resize(max_size);
std::fill(bucket.begin(), bucket.end(), nullptr);
}
std::size_t capacity() const { return bucket.capacity(); }
};
The fill
operation is not necessary though since the resize
operation will zero initialize all the pointers anyway.
CodePudding user response:
Whilst NULL
is a null pointer literal, it isn't required to be an expression of pointer type.
You could use nullptr
, however the resize
has done that for you, and the constructor of std::vector
could have done that too.
#include <cstddef>
#include <string>
#include <vector>
struct node
{
std::string key;
std::string value;
node* next;
};
class HashTable
{
private:
std::vector<node*> bucket;
// we don't need to repeat bucket.capacity() as a data member
public:
HashTable(std::size_t max_size = 100)
: bucket(max_size)
{
// we don't need to reserve, we've constructed a vector of appropriate size
}
};