dimanche 19 juin 2022

why is my custom UnorderedMap container iterator not working?

I am making my own hash map container for my school project and I cant get my iterators to work. This is what my container looks like:

#include <cstddef>
#include <list>
#include <string>
#include <utility>
#include <vector>

template<typename T, typename F>
class UnorderedMap {
  public:
  using key_type = T;
  using mapped_type = F;
  using value_type = std::pair<key_type, mapped_type>;
  using bucket_type = std::list<value_type>;

  UnorderedMap() {
    storage_.resize(storage_size_);
  }

  size_t hash(std::string word) {
    size_t sum = 5381;
    for(char c : word)
      sum = (sum << 4) + c;
    return sum % storage_size_;
  }

  void emplace(const key_type& key, mapped_type value) {
    size_t index = hash(key);
    auto& bucket = storage_[index];
    bucket.push_back(value_type(key, std::move(value)));
  }

  mapped_type& operator[](const key_type& key) {
    auto index = hash(key);
    auto& bucket = storage_[index];
    for(auto& e : bucket) {
      if(e.first == key) {
        return e.second;
      }
    }
    mapped_type temp;
    bucket.push_back(value_type(key, std::move(temp)));
    return bucket.back().second;
  }

  class iterator : public std::iterator<std::random_access_iterator_tag, value_type> {
    public:
    iterator(value_type* p) : p_{p} {} 

    private:
    value_type* p_;
  };

  iterator begin() {
    for(auto& e : storage_) {
      if(!e.empty())
        return e.front();
    }
  }

  iterator& end() {
  }

  private:
  std::vector<bucket_type> storage_;
  size_t storage_size_ = 1000;
};

The problem is with iterator begin(). It say that it can't convert e.front() to an iterator. Everything else is working fine.

Aucun commentaire:

Enregistrer un commentaire