Advertisement
SomniP

Заполнение hash - таблицы

Jan 11th, 2019
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.34 KB | None | 0 0
  1. #include <iostream>
  2. #include <windows.h>
  3. #include <string>
  4. #include <functional>
  5. #include <map>
  6. #include <iomanip>
  7.  
  8. using namespace std;
  9.  
  10. int main()
  11. {
  12.     SetConsoleCP(1251);
  13.     SetConsoleOutputCP(1251);
  14.     system("color 0A");
  15.  
  16.     cout << "Введите количество значений для вставки в hash - таблицу ";
  17.     size_t n;
  18.     cin >> n;
  19.  
  20.     hash<string> hash_function1;
  21.     size_t size_table = 5u;
  22.     auto hash_function2 = [&size_table](string str)
  23.     {
  24.         int sum = 0;
  25.         for (const auto &c : str)
  26.         {
  27.             sum += static_cast<int>(c);
  28.         }
  29.         return abs(sum) % size_table;
  30.     };
  31.  
  32.     multimap<size_t, string> hash_table;
  33.     for (size_t ind = 0u; ind < n; ++ind)
  34.     {
  35.         cout << "Введите значение " << ind + 1u << " элемента" << endl;
  36.         if (cin.rdbuf()->in_avail())
  37.         {
  38.             cin.ignore(cin.rdbuf()->in_avail(), '\n');
  39.         }
  40.         string value;
  41.         getline(cin, value);
  42.         //size_t hash_value = hash_function1(value);
  43.         size_t hash_value = hash_function2(value);
  44.         hash_table.insert(make_pair(hash_value, value));
  45.     }
  46.  
  47.     cout << endl;
  48.     cout << "Содержание hash - таблицы" << endl;
  49.     cout << setw(14u) << "Ключ" << " | Значение" << endl;
  50.     for (const auto &p : hash_table)
  51.     {
  52.         cout << setw(14u) << p.first << " | " << p.second << endl;
  53.     }
  54.     cout << endl;
  55.  
  56.     system("pause");
  57.     return 0;
  58. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement