hello my code is having the following issue:
warning: range-based for loop is a C++11 extension [-Wc++11-extensions]
for this line of code:
for (auto val : myTable[i])
How do I fixed this? I couldn't find anything helpful online so I would appreciate step by step guidance (preferably with pictures but I won't complain).
full code:
#include <iostream>
#include<list>
using namespace std;
class hashtable
{
int capacity;
list<int> *myTable;
public:
hashtable(int capacity)
{
this->capacity = capacity;
myTable = new list<int>[capacity];
}
void setList(int hashedIndex, int value)
{
myTable[hashedIndex].push_back(value);
}
int hashFunction(int value) {
int retVal = (value * 31) % capacity;
return retVal;
}
void insert(int value) {
int hashedIndex = hashFunction(value);
cout << "inserted " << value << endl;
setList(hashedIndex, value);
}
void delete_elem(int value)
{
if (search(value))
{
int hashedIndex = hashFunction(value);
myTable[hashedIndex].remove(value);
}
}
bool search(int value)
{
int hashedIndex = hashFunction(value);
list<int> ::iterator tmp;
for (list<int> ::iterator itr = myTable[hashedIndex].begin(); itr != myTable[hashedIndex].end(); itr++)
{
if (*itr == value)
{
tmp = itr;
break;
}
}
return tmp != myTable[hashedIndex].end();
}
void printContents() {
cout << "trying to print contents" << endl;
for (int i = 0; i < capacity; i++)
{
cout << i;
for (auto val : myTable[i]) // issue is on this line
{
cout << " --> " << val;
}
cout << endl;
}
cout << endl;
}
};
int main(int argc, char const *argv[])
{
hashtable ht(10);
for (int i = 0; i < 10; i++) {
ht.insert(i);
}
ht.printContents();
// cout << "yo" << endl;
return 0;
}
The IDE I'm using is NetBeans
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…