//#include "stdafx.h"
#include <iostream>
#include <vector>
#include <map>
#include <list>
#include <iostream>
#include <vector>
#include <list>
#include <cstdlib>
#include <ctime>
using namespace std;
template<typename T>
class HashTable
{
private:
int size;
vector <list<T>> myMap;
int hashKeyFunc(int key) {
return key % size;
}
public:
HashTable(int size) {
this->size = size;
this->myMap.resize(size);
};
~HashTable() {};
void add(int key, T value)
{
int hashKey = this->hashKeyFunc(key);
myMap.at(hashKey).push_back(value);
}
T find(int key) {
int hashKey = this->hashKeyFunc(key);
auto i = this->myMap.at(hashKey).begin();
while (i != this->myMap.at(hashKey).end()) {
if (*i == key) return *i;
++i;
}
//throw std::exception("There is no such element in the hash table");
}
};
int main() {
srand(time(NULL));
int size, position;
cout << "Enter size of table" << endl;
cin >> size;
HashTable<int> testHashTable(size);
int *Keys = new int[size];
Keys[0] = rand() % 100 + 1;
for (int i = 1; i < size; i++) {
Keys[i] = Keys[i] + (rand() % 100 + 1);
}
for (int i = 0; i < size; i++) {
testHashTable.add(Keys[i], Keys[i]);
}
cout << testHashTable.find(5) << endl;
cout << "Finish" << endl;
}
Не совсем корректно работает программа для поиска индекса элемента по ключу
Сборка персонального компьютера от Artline: умный выбор для современных пользователей