1

пытаюсь написать hashmap, идея в том, что я объявляю std::vector<std::list<std::pair<int, std::string>>> hash_map; . То есть, у меня имеется вектор, каждый элемент которого - список, а элементы этого списка пары (int key, std::string), этот список нужен для разрешения коллизий. Проблема в том, что я даже не могу инициализировать данное творение). Соответственно я не могу и вывести всё это на экран. Кто в этом разбирается, помогите пожалуйста.

Ошибка на 15 строчке кода в файле Vhash.cpp

E0304 отсутствуют экземпляры перегруженная функция "std::vector<_Ty, _Alloc>::push_back [с _Ty=std::list<std::pair<int, std::string>, std::allocator<std::pair<int, std::string>>>, _Alloc=std::allocator<std::list<std::pair<int, std::string>, std::allocator<std::pair<int, std::string>>>>]", соответствующие списку аргументов

\Vhash.h

#pragma once
#ifndef _VHASH_H_
#define _VHASH_H_
#include <iostream>
#include <string>
#include <vector>
#include <list>
class Vhash
{
public:
    Vhash()
    {
        
        hash_map.resize(10);
        amount = hash_map.size();
        size = hash_map.capacity();
    }
    ~Vhash() {}
    void show_hash_map();
    void put(int, std::string);
    void get(int);
    void get(std::string);
    void remove(std::string);
    void remove(int);
    void init_map();


private:
    std::vector <std::list <std::pair <int, std::string>>> hash_map;
    int amount;
    int size;
};
#endif

Vhash.cpp

#include "Vhash.h"

void Vhash::show_hash_map()
{
    return;
}

void Vhash::init_map()
{
    int temp = 0;
    std::string tmp = "data";
    std::list<std::pair <int, std::string>> help;
    for (int i = 0; i < size; ++i)
    {
        hash_map.push_back(help.push_back(std::make_pair(temp, tmp)));
    }
2
  • А unordered_map приспособить не хотите?...
    – Harry
    25 дек 2020 в 16:40
  • Спасибо за ответ, но моё задание заключается в том, чтобы самому написать такую таблицу :( Да и тогда от коллизий пришлось бы избавляться по-другому.
    – andrew_aak
    25 дек 2020 в 16:47

1 ответ 1

0

std::list::push_back(...) возвращает void, т.е. вы не можете один push_back в другой вложить тут hash_map.push_back(help.push_back(std::make_pair(temp, tmp)));.

Нужно сделать в две строки:

help.push_back(std::make_pair(temp, tmp));
hash_map.push_back(help);

Затем вывести в консоль эту составную структуру можно например так:

for (auto const & a: hash_map) {
    for (auto const & b: a)
        std::cout << b.first << ": " << b.second << ", ";
    std::cout << " | ";
}
4
  • Спасибо большое.
    – andrew_aak
    25 дек 2020 в 17:10
  • А что на счёт, вывода на экран, просто std::cout << hash_map[i] << std::endl; естественно не работает
    – andrew_aak
    25 дек 2020 в 17:17
  • @mld8nk Составные структуры не выводятся просто так в консоль, надо обходить их в цикле, например как for (auto const & a: hash_map) for (auto const & b: a) std::cout << b.first << ": " << b.second << ", ";
    – Arty
    25 дек 2020 в 18:27
  • Спасибо вам огрномное
    – andrew_aak
    25 дек 2020 в 19:03

Ваш ответ

By clicking “Отправить ответ”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Всё ещё ищете ответ? Посмотрите другие вопросы с метками или задайте свой вопрос.