Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <windows.h>
- #include <string>
- #include <functional>
- #include <map>
- #include <iomanip>
- using namespace std;
- int main()
- {
- SetConsoleCP(1251);
- SetConsoleOutputCP(1251);
- system("color 0A");
- cout << "Введите количество значений для вставки в hash - таблицу ";
- size_t n;
- cin >> n;
- hash<string> hash_function1;
- size_t size_table = 5u;
- auto hash_function2 = [&size_table](string str)
- {
- int sum = 0;
- for (const auto &c : str)
- {
- sum += static_cast<int>(c);
- }
- return abs(sum) % size_table;
- };
- multimap<size_t, string> hash_table;
- for (size_t ind = 0u; ind < n; ++ind)
- {
- cout << "Введите значение " << ind + 1u << " элемента" << endl;
- if (cin.rdbuf()->in_avail())
- {
- cin.ignore(cin.rdbuf()->in_avail(), '\n');
- }
- string value;
- getline(cin, value);
- //size_t hash_value = hash_function1(value);
- size_t hash_value = hash_function2(value);
- hash_table.insert(make_pair(hash_value, value));
- }
- cout << endl;
- cout << "Содержание hash - таблицы" << endl;
- cout << setw(14u) << "Ключ" << " | Значение" << endl;
- for (const auto &p : hash_table)
- {
- cout << setw(14u) << p.first << " | " << p.second << endl;
- }
- cout << endl;
- system("pause");
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement